Open changju opened 2 years ago
<project xmlns="...">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study.springboot</groupId>
<artifactId>spring-boot-chap3-10-autoconfiguration-2-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
@Configuration
@EnableConfigurationProperties(PrettyFatherProperties.class)
public class PrettyFatherConfiguration {
@Bean
@ConditionalOnMissingBean // 등록이 되어진 bean 이 없으면 등록 하여
public PrettyFather prettyFather(PrettyFatherProperties properties) {
PrettyFather pFather = new PrettyFather();
pFather.setName(properties.getName());
pFather.setOld(properties.getOld());
return pFather;
}
}
@Getter
@Setter
@ToString
@ConfigurationProperties("prettyfather")
public class PrettyFatherProperties {
String name;
int old;
}
Properties 설정
prettyfather.name = changjulee
prettyfather.old = 20
Autowired
@Component
public class PrettyFatherRunner implements ApplicationRunner{
@Autowired
PrettyFather prettyFather;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(prettyFather);
}
}
@ Bean
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication application = new SpringApplication(Application.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
}
/*
* ComponentScan 으로 Bean으로 등록이 된다.
* EnableAutoConfiguration 에서 등록을 하기 때문에 오버라이드 된다.
* @ConditionalOnMissingBean의 의미는 Bean의 정의가 안되어 있다면, 내가 정의한 Bean은 사용 할 수 없다.
*/
@Bean
public PrettyFather prettyFather()
{
PrettyFather prettyFather = new PrettyFather();
prettyFather.setName("cblee");
prettyFather.setOld(29);
return prettyFather;
}
}
3부: 스프링 부트 원리
11. 자동 설정 만들기 2부: @ ConfigurationProperties
덮어쓰기 방지하기
빈 재정의 수고 덜기