配置Bean的初始化和销毁的方法:
配置初始化和销毁的方法:
* init-method=”setup”
* destroy-method=”teardown”
执行销毁的时候,必须手动关闭工厂,而且只对scope=”singleton”有效.
##### Bean的生命周期的11个步骤:
1. instantiate bean对象实例化
2. populate properties 封装属性
3. 如果Bean实现BeanNameAware 执行 setBeanName
4. 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
5. 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6. 如果Bean实现InitializingBean 执行 afterPropertiesSet
7. 调用<bean init-method="init"> 指定初始化方法 init
8. 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
9. 执行业务处理
10. 如果Bean实现 DisposableBean 执行 destroy
11. 调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy
在CustomerService类的add方法之前进行权限校验?
<!-- demo4 bean生命周期--> <bean id="customerservice" class="cn.spring.demo4.CustomerServiceimpl" init-method="setup" destroy-method="teardown"> <property name="name" value="<property name="name" value="">"/> </bean> <bean class="cn.spring.demo4.myBeanPostProcessor"></bean> <!-- demo4 bean生命周期-->
CustomerService.java 接口函数
package cn.spring.demo4; public interface CustomerService { public void add(); public void find(); }
CustomerServiceimpl.java
package cn.spring.demo4; import org.springframework.beans.BeansException; 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; public class CustomerServiceimpl implements CustomerService,BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean { private String name; //实现自己业务逻辑 public void add() { System.out.println("第9步添加用户"); } public void find() { System.out.println("第9步查找用户"); } public void setName(String name){ System.out.println("第2步: 封装属性"+name); this.name = name; } public CustomerServiceimpl() { super(); // TODO Auto-generated constructor stub System.out.println("第1步:对象实例化"); } public void setBeanName(String name) { // TODO Auto-generated method stub System.out.println("第3步:注入配置文件名称"+name); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub System.out.println("第4步:注入ApplicationContext"+applicationContext); } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("第6步:属性设置后执行……"); } public void setup(){ System.out.println("第7步:调用手动设置的初始化方法"); } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("第10步:调用的销毁的方法"); } public void teardown(){ System.out.println("第11步:调用手动设置的销毁方法"); } }
myBeanPostProcessor.java package cn.spring.demo4; import java.lang.reflect.Method; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.cglib.proxy.InvocationHandler; import org.springframework.cglib.proxy.Proxy; public class myBeanPostProcessor implements BeanPostProcessor { /* * @bean:实例对象 * * @beanName:在配置文件中配置的类的标识 */ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // 可以在这里增强 System.out.println("第5步,初始化之前"); return bean; } public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { System.out.println("第8步:初始化后执行..."); // 动态代理: if (beanName.equals("customerservice")) { Object proxy = Proxy.newProxyInstance(bean.getClass() .getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() { // 调用目标方法的时候,调用invoke方法. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("add".equals(method.getName())) { System.out.println("权限校验..."); Object result = method.invoke(bean, args); System.out.println(System.currentTimeMillis()); return result; } return method.invoke(bean, args); } }); return proxy; } return bean; } }
package cn.spring.demo4; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest4 { @Test public void demo1() { ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext( "applicationContext.xml"); CustomerService customerservice=(CustomerService) applicationcontext.getBean("customerservice"); customerservice.add(); customerservice.find(); applicationcontext.close(); } }