@Bean @ConditionalOnMissingBean public Holoman holoman(){ Holomanholoman=newHoloman(); holoman.setHowLong(5); holoman.setName("maple");
return holoman; } }
@ConditionalOnMissingBean을 사용하면 해당 Bean이 없는 경우에만 EnableAutoConfiguration을 통해 Bean이 추가 된다.
결과
2020-03-15 21:31:49.936 INFO 67228 --- [ main] com.example.Application : Starting Application on yangdong-uui-MacBookPro.local with PID 67228 (/Users/dongwoo-yang/dev/study/spring/everyday_spring/chapter3/ch3_5/springbootgettingstarted/target/classes started by dongwoo-yang in /Users/dongwoo-yang/dev/study/spring/everyday_spring/chapter3/ch3_5/springbootgettingstarted) 2020-03-15 21:31:49.938 INFO 67228 --- [ main] com.example.Application : No active profile set, falling back to default profiles: default 2020-03-15 21:31:50.581 INFO 67228 --- [ main] com.example.Application : Started Application in 0.937 seconds (JVM running for 1.598) Holoman{name='동장군', howLong=60}
Process finished with exit code 0
springbootgetttingstarted프로젝트의 Bean이 출력됨을 확인할 수 있다.
application.properties를 이용한 방법
springbootgetttingstarted 프로젝트 src/resource에 application.properties파일을 생성
holoman.name = 아 집에 가고 싶다. holoman.how-long = 10000
@Bean @ConditionalOnMissingBean public Holoman holoman(HolomanProperties properties){ Holomanholoman=newHoloman(); holoman.setHowLong(properties.getHowLong()); holoman.setName(properties.getName());
return holoman; } }
@EnableConfigurationProperties를 추가 Bean을 만들 때 HolomanProperties를 참조해서 Bean을 생성한다. springbootgetttingstarted의 application.properties를 가져다가 Bean을 생성한다.
springbootgetttingstarted내의 holoman Bean을 없애주어야 한다…. 오류 잡느라 시간 오래 걸렸다.
결과
2020-03-1522:07:53.804 INFO 70087 --- [ main] com.example.Application : Starting Application on yangdong-uui-MacBookPro.local with PID 70087 (/Users/dongwoo-yang/dev/study/spring/everyday_spring/chapter3/ch3_5/springbootgettingstarted/target/classes started by dongwoo-yang in /Users/dongwoo-yang/dev/study/spring/everyday_spring/chapter3/ch3_5/springbootgettingstarted) 2020-03-1522:07:53.808 INFO 70087 --- [ main] com.example.Application : No active profile set, falling back to default profiles: default 2020-03-1522:07:54.628 INFO 70087 --- [ main] com.example.Application : Started Application in 1.148 seconds (JVM running for1.631) Holoman{name='A... I want to go home', howLong=10000}