You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
postProcessBeforeInitalization : 객체 생성 이후에 @PostConstruct 같은 초기화가 발생하기 전에 호출되는 포스트 프로세서이다.
postProcessAfterInitialization : 객체 생성 이후에 @PostConstruct 같은 초기화가 발생한 다음에 호출되는 포스트 프로세서이다.
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//packageorg.springframework.beans.factory.config;
importorg.springframework.beans.BeansException;
importorg.springframework.lang.Nullable;
publicinterfaceBeanPostProcessor {
@NullabledefaultObjectpostProcessBeforeInitialization(Objectbean, StringbeanName) throwsBeansException {
returnbean;
}
@NullabledefaultObjectpostProcessAfterInitialization(Objectbean, StringbeanName) throwsBeansException {
returnbean;
}
}
➡️ BasicPostProcessorTest
AToBPostProcessor
빈 후처리기이다. 인터페이스인 BeanPostProcessor를 구현하고, 스프링 빈으로 등록하면 스프링 컨테이너 빈 후처리기로 인식하고 동작한다.
이 빈 후처리기는 A 객체를 새로운 B 객체로 바꿔치기 한다. 파라미터로 넘어오는 빈(bean) 객체가 A 인스턴스이면 새로운 B 객체를 생성해서 반환한다. 여기서 A 대신에 반환된 값인 B가 스프링 컨테이너에 등록된다. 다음 실행결과를 보면 beanName=beanA, bean=A 객체의 인스턴스가 빈 후처리기에 넘어온 것을 학인할 수 있다.
B b = applicationContext.getBean("beanA", B.class)실행 결과를 보면 최종적으로"beanA"라는 스프링 빈 이름에A객체 대신에B객체가 등록된 것을 확인할 수 있다.A` 는 스프링 빈으로 등록조차 되지 않는다.
@PostConstruct` 는 스프링 빈 생성 이후에 빈을 초기화 하는 역할을 한다. 그런데 생각해보면 빈의 초기화 라는 것이 단순히 @PostConstruct 애노테이션이 붙은 초기화 메서드를 한번 호출만 하면 된다. 쉽게 이야기해서 생성된 빈을 한번 조작하는 것이다.
따라서 빈을 조작하는 행위를 하는 적절한 빈 후처리기가 있으면 될 것 같다.
스프링은 CommonAnnotationBeanPostProcessor 라는 빈 후처리기를 자동으로 등록하는데, 여기에서 @PostConstruct 애노테이션이 붙은 메서드를 호출한다. 따라서 스프링 스스로도 스프링 내부의 기능을 확장하기 위해 빈 후처리기를 사용한다.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
섹션8. 빈 후처리기
1️⃣ 빈 후처리기 - 소개
➡️ 빈 후처리기 - BeanPostProcessor
➡️ 빈 후처리기 과정
✅ 빈 등록 과정을 빈 후처리기
2️⃣ 빈 후처리기 - 예제 코드 1
➡️ 일반적인 스프링 빈 등록 과정 1
3️⃣ 빈 후처리기 - 예제 코드 2
✅ 빈 후처리기 적용
➡️ BeanPostProcessor 인터페이스 - 스프링 제공
➡️ BasicPostProcessorTest
실행 결과를 보면 최종적으로"beanA"라는 스프링 빈 이름에A객체 대신에B객체가 등록된 것을 확인할 수 있다.A` 는 스프링 빈으로 등록조차 되지 않는다.🗒️정리
All reactions