配置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(); } }