您的位置:首页 > 编程语言 > Java开发

Spring容器中Bean的生命周期(init-method destroy-method)

2017-06-14 10:57 393 查看
Spring容器中Bean的生命周期



链接:http://www.cnblogs.com/zrtqsk/p/3735273.html

这一篇很详细的讲了Bean生命周期的每一个过程。

 

    我主要是想实现一下init方法核destory方法,因为这个和AOP编程的环绕通知有点儿相似的感觉,所以特别来研究一下这两个方法。

     在Spring配置中,init-method 用于配置初始化方法,准备数据等,destroy-method 用于配置销毁方法,清理资源等。写法为:

<bean id=""class="" init-method="初始化方法名称"  destroy-method="销毁的方法名称">
    下面编写代码来实现一下:

第一种方法,直接编写init-method和destroy-method:

    编写UserService接口,编写接口的实现类(在实现类里编写init方法核destroy方法),配置xml,编写测试类。【注意,这里需要加上close方法,不然destory方法的输出不会显示。】

/**
*
*/
package com.Lily.SpringLearning.c_lifeCycle1;

/**
* *
* @author   LilyLee
* @date     2017年6月14日
* @time     上午9:10:25
* @Version  1.0
* @email    lilylee_1213@foxmail.com
*
*/
public interface UserService {
public void addUser();
}

/**
*
*/
package com.Lily.SpringLearning.c_lifeCycle1;

/**
* *
* @author   LilyLee
* @date     2017年6月14日
* @time     上午9:11:13
* @Version  1.0
* @email    lilylee_1213@foxmail.com
*
*/
public class UserServiceImpl implements UserService {

@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("addUser()");
}

public void myInit(){
System.out.println("init()");
}

public void myDestory(){
System.out.println("destory()!");
}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="userServiceId" class="com.Lily.SpringLearning.c_lifeCycle1.UserServiceImpl" init-method="myInit" destroy-method="myDestory"></bean>

</beans>

/**
*
*/
package com.Lily.SpringLearning.c_lifeCycle1;

import static org.junit.Assert.*;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* *
* @author   LilyLee
* @date     2017年6月14日
* @time     上午10:21:24
* @Version  1.0
* @email    lilylee_1213@foxmail.com
*
*/
public class Test {

@org.junit.Test
public void test() {
String xmlPath ="com/Lily/SpringLearning/c_lifeCycle1/beans.xml";
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
UserService u=(UserService) ac.getBean("userServiceId");
u.addUser();
ac.close();
}

}


 

第二种方法,实现BeanPostProcessor接口。

    spring提供一种机制,只要实现此接口BeanPostProcessor,并将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after()。

    spring提供工厂勾子(Factory hook),用于修改实例对象,可以生成代理对象,是AOP底层。

    与第一种方法的不同是,

    (1)增加了一个实现BeanPostProcessor接口的MyBeanPostProcessor类。这里的后方法的return 需要用生成jdk代理的方式实现:

return Proxy.newProxyInstance(loader, interfaces, h)
    (2)配置xml的时候不需要再写init那句了,后面只有一句:<bean class=”……”>

/**
*
*/
package com.Lily.SpringLearning.c_lifecycle2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
* *
* @author   LilyLee
* @date     2017年6月14日
* @time     上午10:34:29
* @Version  1.0
* @email    lilylee_1213@foxmail.com
*
*/
public class MyBeanPostProcessor implements BeanPostProcessor{

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("前方法: "+beanName);
return bean;
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("后方法:  "+beanName);
return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(),
bean.getClass().getInterfaces(),
new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("----start");
//执行目标方法
Object o=method.invoke(bean, args);
System.out.println("----submint");
return o;
}
});

}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="userServiceId" class="com.Lily.SpringLearning.c_lifecycle2.UserServiceImpl" ></bean>
<bean class="com.Lily.SpringLearning.c_lifecycle2.MyBeanPostProcessor"></bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring JAVA