public class MyBean implements BeanPostProcessor {
public void say(){
System.out.printf("hello");
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/spring-config.xml"});
MyBean myBean = context.getBean(MyBean.class);
myBean.say();
}
}
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<bean id="myBean" class="com.alibaba.dubbo.demo.MyBean" />
运行结果:
[05/02/18 05:08:53:053 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6d9c638: startup date [Mon Feb 05 17:08:53 CST 2018]; root of context hierarchy
[05/02/18 05:08:53:053 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [spring/spring-config.xml]
inithello
Process finished with exit code 0
期待的市BeanPostProcessor里的before和after方法被调用,但实际没有。有谁知道为什么吗?