Java Bean 注册对象

注册对象

POM.xml

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.6</version>
</dependency>

ElasticSearchConfig.java

package com.vipsoft.web.config;

import cn.hutool.core.date.DateUtil;
import com.vipsoft.web.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ElasticSearchConfig {

    @Bean
    public Person client() {
        Person person = new Person();
        person.setName(DateUtil.now());
        return person;
    }
}

EsIndexTest.java

package com.vipsoft.web;

import com.vipsoft.web.entity.Person;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class EsIndexTest {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    Person person;

    @Test
    void connectTest() throws Exception {

        logger.info(person.getName());
    }
}

image

posted @ 2023-03-23 09:51  VipSoft  阅读(55)  评论(0编辑  收藏  举报