BeanPostProcessor方法未被调用

时间:2018-03-27 10:21:29

标签: java spring

我正在尝试理解静态初始化,实例初始化,InitializingBean和BeanPostProcessor的顺序,所以我创建了下面的示例,除了BeanPostProcessor没有被调用之外,一切正常,我错过了什么?

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

@Component
public class SpringTest {

    @Autowired
    Person person;

    public static void main(String[] args) {
        testApplicationContext();
    }

    private static void testApplicationContext() {
        // here static and instance initializers of Person class will be invoked right away, even when we are not calling getBean method
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\E_Drive\\Projects\\Workspace\\Test\\CS101\\src\\com\\learn\\stackoverflow\\general\\bean.xml");
        ApplicationContext applicationContext2 = new FileSystemXmlApplicationContext("C:\\E_Drive\\Projects\\Workspace\\Test\\CS101\\src\\com\\learn\\stackoverflow\\general\\bean.xml");

        // notice that above you are creating 2 ioc containers, and since they are different ioc containers so it is possible to have 2 singletons in JVM.

        SpringTest springTest = (SpringTest) applicationContext.getBean("springTest");

    }


}

人员类:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value="singleton")
public class Person implements InitializingBean, BeanPostProcessor {
    @Autowired
    SpringTest springTest;

    static{
        System.out.println("Static initialization");
    }

    {
        System.out.println("Instance initialization");
    }

    public void sayHello(){
        System.out.println("Hello");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean.afterPropertiesSet");
    }

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("BeanPostProcessor.postProcessAfterInitialization : " + arg1);
        return arg0;
    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("BeanPostProcessor.postProcessBeforeInitialization : " + arg1);
        return arg0;
    }

}

Bean XML文件:

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.learn.stackoverflow.general"/>
    <context:annotation-config />

   <!-- <bean id = "person" class = "com.learn.stackoverflow.general.Person" scope="singleton">
   </bean> -->

   <bean id = "springTest" class = "com.learn.stackoverflow.general.SpringTest" scope="singleton">
   </bean>

</beans>

输出:

Static initialization
Instance initialization
InitializingBean.afterPropertiesSet
Instance initialization
InitializingBean.afterPropertiesSet

是否还需要调用BeanPostProcessor方法?

1 个答案:

答案 0 :(得分:1)

一个bean不能自我后期处理,后处理器需要是一个单独的bean; see tutorial