# 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
// 소문자 가능함.
@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. 결과
'[Spring] > springboot' 카테고리의 다른 글
[SpringBoot] 멀티 데이터베이스 설정하기 - Gradle 설정편 (0) | 2022.05.22 |
---|---|
[SpringBoot] 공공데이터포털 활용 과거 날씨 시간별 정보 받기 (0) | 2022.04.16 |
[공공데이터활용] 공휴일 정보 가져오기(JAVA 버전) (0) | 2022.04.06 |
[SpringBoot] RestAPI 파일업로드 - 2 (0) | 2022.02.24 |
[SpringBoot] RestAPI 파일업로드 - 1 (0) | 2022.02.24 |