'springboot Autoconfiguration'에 해당되는 글 1건


 

# RegBean.java

package com.example.demo;

public class RegBean {

    private String beanName;
    private int beanInt;

    public void setBeanInt(int beanInt) {
        this.beanInt = beanInt;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public int getBeanInt() {
        return beanInt;
    }

    public String getBeanName() {
        return beanName;
    }

    @Override
    public String toString() {
        return "RegBean{" +
                "beanName='" + beanName + '\'' +
                ", beanInt=" + beanInt +
                '}';
    }
}

# RegBeanConfiguration.java

@Configuration
@EnableConfigurationProperties(RegBeanProperties.class)
public class RegBeanConfiguration {

    @Bean
    @ConditionalOnMissingBean // 이 타입에 Bean 없을 때만 등록
    public RegBean regBean(RegBeanProperties properties) {
        RegBean regBean = new RegBean();
        regBean.setBeanInt(properties.getBeanInt());
        regBean.setBeanName(properties.getBeanName());
        return regBean;
    }
}

# RegBeanProperties.java

pom.xml에 등록해야함.

 

// 소문자 가능함.
@ConfigurationProperties("reg-bean")
public class RegBeanProperties {

    private String beanName;
    private int beanInt;

    public void setBeanInt(int beanInt) {
        this.beanInt = beanInt;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public int getBeanInt() {
        return beanInt;
    }

    public String getBeanName() {
        return beanName;
    }

}

# Maven Install


 

1. pom.xml 특정 jar 가져오기

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>AutoconfigurationSub</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

 

2.  가져온 Bean 값 활용하기 

@Component
public class RegBeanRunner implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(RegBeanRunner.class);

    @Autowired
    RegBean regBean;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        logger.info("regBean=" + regBean.toString());
    }
}

 

3. application.properties에서 프로퍼티즈 등록하기 

logging.level.root=debug
reg-bean.beanInt=10
reg-bean.beanName=RegBeanTest

 

4. 결과 

 

 

블로그 이미지

미나미나미

,