1,BeanPostProcessor接口;
不能在POJO上面使用,需要单独找一个类进行使用;
如果在POJO上面实现了此接口,在实现了其他*Aware
接口之后,这个接口方法将不会被调用;
2, POJO实现了BeanNameAware接口;
可以与BeanPostProcessor同时使用;
3, POJO实现了BeanFactoryAware接口;
可以与BeanPostProcessor同时使用;
4, POJO实现了ApplicationContextAware接口;
可以与BeanPostProcessor同时使用;
5,POJO实现了DisposableBean接口;
可以正常调用接口的dispose()方法;
6,在bean声明部分定义的init-method
和destroy-method方法可以正常调用;
7,如果调用dispose()、destroy-method方法,
需要使用
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("appbeans.xml");
//...
context.registerShutdownHook();
声明周期图:
http://my.oschina.net/u/218421/blog/37743
package com.stono.sprtest; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Minstrel implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "appbeans.xml"); Minstrel minstrel = (Minstrel) context.getBean("minstrel"); minstrel.show(); context.registerShutdownHook(); } private String accordion; private Logger logger = Logger.getLogger(Minstrel.class); private String song; public Minstrel() { logger.info("default constructor"); } public Minstrel(String song) { logger.info("song constructor"); this.song = song; } public Minstrel(String song, String accordion) { logger.info("song accordion constructor"); this.song = song; this.accordion = accordion; } public void destroyMethod() { logger.info("destroyMethod called"); } public String getAccordion() { logger.info("getAccordion method"); return accordion; } public String getSong() { logger.info("getSong method"); return song; } public void initMethod() { logger.info("initMethod called"); } public void setAccordion(String accordion) { logger.info("setAccordion method"); this.accordion = accordion; } public void setSong(String song) { logger.info("setSong method"); this.song = song; } public void show() { System.out.println("show method called"); } @Override public void setBeanName(String name) { logger.info("com.stono.sprtest.Minstrel.setBeanName(String):" + name); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { logger.info("com.stono.sprtest.Minstrel.setBeanFactory(BeanFactory):" + beanFactory); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { logger.info("com.stono.sprtest.Minstrel.setApplicationContext(ApplicationContext):" + applicationContext); } @Override public void afterPropertiesSet() throws Exception { logger.info("com.stono.sprtest.Minstrel.afterPropertiesSet()"); } @Override public void destroy() throws Exception { logger.info("com.stono.sprtest.Minstrel.destroy()"); } }
package com.stono.sprtest; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class AppBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization is called"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization is called"); return bean; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="minstrel" class="com.stono.sprtest.Minstrel" init-method="initMethod" destroy-method="destroyMethod"> <constructor-arg name="song" value="SongSong"></constructor-arg> <property name="accordion"> <value>oldAccordion</value> </property> </bean> <bean class="com.stono.sprtest.AppBeanPostProcessor"></bean> </beans>
时间: 2024-11-05 18:33:12