Spring 의 BeanFactory PostProcessor 인터페이스 소개

이전 글 Spring 의 BeanPostProcessor (백 엔 드 프로세서) 소 개 를 바탕 으로 BeanFactory PostProcessor 인 터 페 이 스 를 소개 합 니 다. 이름 으로 볼 때 비슷 하지만 그들의 역할 은 다 릅 니 다.
글 목록
  • BeanFactoryPostProcessor
  • 1. BeanFactory PostProcessor 인터페이스 내용
  • 2. 사용자 정의 BeanFactory PostProcessor 구현 클래스
  • 3. 프로필 에 등록
  • 4. 테스트
  • BeanFactoryPostProcessor
        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 의 차 이 를 알 수 있다.

    좋은 웹페이지 즐겨찾기