즉, 11. postProcess**Before**Initialization 단계에서 @Autowired가 붙은 의존성을 주입한다.
PostConstruct
@ServicepublicclassBookService { @AutowiredBookRepository myBookRepository;// 이제 Runner는 필요없다. @PostConstructpublicvoidsetup() {System.out.println(myBookRepository.getClass()); }}
@PostConstruct 단계에서는 이미 빈이 주입된 상태다.
12. InitializingBean's afterPropertiesSet 단계에서 작업한다.
이제 Runner 없이도 정보를 출력할 수 있다. 출력되는 위치만 조금 다르다.
2020-03-16 13:33:18.519 WARN 14600 --- [ main] o.s.boot.StartupInfoLogger : InetAddress.getLocalHost().getHostName() took 5005 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2020-03-16 13:33:23.524 INFO 14600 --- [ main] me.whiteship.autowired.DemoApplication : Starting DemoApplication on macbook.local with PID 14600 (/Users/sojeong/dev/IntelliJ-workspace/spring-keesun-baik/spring-framework-core-02-autowired/target/classes started by sojeong in /Users/sojeong/dev/IntelliJ-workspace/spring-keesun-baik)
2020-03-16 13:33:23.525 INFO 14600 --- [ main] me.whiteship.autowired.DemoApplication : No active profile set, falling back to default profiles: default
2020-03-16 13:33:24.073 INFO 14600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-16 13:33:24.079 INFO 14600 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-16 13:33:24.079 INFO 14600 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-16 13:33:24.121 INFO 14600 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-16 13:33:24.121 INFO 14600 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 566 ms
// @PostConstruct가 출력되는 위치
class me.whiteship.autowired.MyBookRepository
2020-03-16 13:33:24.219 INFO 14600 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-16 13:33:24.342 INFO 14600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-16 13:33:24.347 INFO 14600 --- [ main] me.whiteship.autowired.DemoApplication : Started DemoApplication in 16.071 seconds (JVM running for 21.668)
// Runner가 출력되는 위치
Runner는 구동이 다 되었을 때 일을 한다.
반면, @PostConstruct는 12. InitializingBean's afterPropertiesSet에 해당한다.
정리
빈 팩토리가 BeanPostProcessor 타입의 빈을 찾는다.
그 중 하나인 AutowiredAnnotationBeanPostProcessor를 찾는다.
AutowiredAnnotationBeanPostProcessor가 일반적인 빈들에게 beanPostProcessor를 적용한다.