Spring 의 BeanFactory PostProcessor 인터페이스 소개
12620 단어 SPRING 시리즈#SPRING-FRAMKER
글 목록
Spring IoC 용 기 는 BeanFactory PostProcessor 가 용기 에서 모든 bean 을 예화 하기 전에 bean 의 정의 (메타 데이터 설정) 를 읽 고 수정 할 수 있 도록 합 니 다.또한 BeanFactory PostProcessor 를 정의 할 수 있 습 니 다. 'order' 속성 을 설정 하여 각 BeanFactory PostProcessor 의 실행 순 서 를 확인 할 수 있 습 니 다. BeanFactory PostProcessor 인 스 턴 스 를 등록 하려 면 BeanFactory PostProcessor 인 스 턴 스 를 실현 하기 위해 자바 류 를 정의 하고 이 인터페이스의 post Processor BeanFactory 방법 을 다시 써 야 합 니 다.beanFactory 를 통 해 bean 의 정의 정 보 를 얻 을 수 있 고 bean 의 정의 정 보 를 수정 할 수 있 습 니 다.이 점 은 빈 포스트 프로세서 와 가장 큰 차이 점 으로 사례 코드 는 지난 글 을 바탕 으로 검증 되 었 다.
1. BeanFactory PostProcessor 인터페이스 내용
public interface BeanFactoryPostProcessor {
/**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for overriding or adding
* properties even to eager-initializing beans.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
2. 사용자 정의 BeanFactory PostProcessor 구현 클래스
/**
* BeanFactoryPostProcessor
*
* @author dengp
*
*/
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
/**
* Bean ,
* beanFactory bean ,
* bean 。 BeanPostProcessor
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println(">> BeanFactoryPostProcessor ");
String[] names = beanFactory.getBeanDefinitionNames();
for (String name : names) {
if("user".equals(name)){
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(name);
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
// MutablePropertyValues , ,
if(propertyValues.contains("name")){
propertyValues.addPropertyValue("name", "bobo");
System.out.println(" ");
}
}
}
System.out.println(">> BeanFactoryPostProcessor ");
}
}
3. 프로필 에 등록
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.dpb.pojo.User" id="user" init-method="start">
<property name="name" value=" ">property>
bean>
<bean class="com.dpb.processor.MyBeanPostProcessor"/>
<bean class="com.dpb.factoryprocessor.MyBeanFactoryPostProcessor">bean>
beans>
4. 테스트
@Test
public void test1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = ac.getBean(User.class);
System.out.println(user);
}
출력 결과
>> BeanFactoryPostProcessor
>> BeanFactoryPostProcessor
User
:bobo
A before-- bean :User [id=0, name=bobo, beanName=null] user
User
A after... bean :User [id=0, name=bobo, beanName=null] user
User [id=0, name=bobo, beanName=null]
요약: 출력 결 과 를 통 해 알 수 있 듯 이 postprocessBeanFactory 방법 이 실 행 된 순 서 는 Bean 의 정례 화 전에 있 습 니 다.Bean 의 속성 치 를 수정 할 수 있 습 니 다.여기 서도 BeanFactory PostProcessor 와 BeanPostProcessor 의 차 이 를 알 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Git에서 개발 환경을 정리해 보았습니다.로컬에 작업 디렉토리 만들기 mkdir [ワーキングディレクトリ名] 작업 디렉토리로 이동 cd [ワーキングディレクトリ名] 작업 디렉토리 초기화 git init git로 연결할 원격 리포지토리를 만듭니다. 이 때 REA...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.