Bean的生命周期按照粗略的五步的话:
需要手动关闭spring容器
//销毁Bean 必须手动关闭Spring容器
ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;
context.close();
需要手动指定初始化方法和销毁方法
init-method="initBean" destroy-method="destroyBean"
无参构造 name属性以及set方法
public class User {
private String name;
public void setName(String name) {
System.out.println("第二步:给对象的属性赋值。");
this.name = name;
}
public User() {
System.out.println("第一步:无参数构造方法执行。");
}
public void initBean(){
System.out.println("第三步:初始化Bean。");
}
public void destroyBean(){
System.out.println("第五步:销毁Bean。");
}
}
<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="user" class="com.bjpowernode.spring.bean.User"
init-method="initBean" destroy-method="destroyBean">
<property name="name" value="小白">property>
bean>
beans>
@Test
public void testBeanLifeCircle(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println("第四步:使用Bean:"+user);
//销毁Bean 必须手动关闭Spring容器
ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;
context.close();
}
在以上的五步中,第三步是初始化Bean,如果还想在初始化前和初始化后添加代码,可以加入”Bean后处理器“ ----”BeanPostProcessor“
Bean生命周期七步:
编写一个类实现BeanPostProcessor类,并且重写before和after方法:
出现return 报错的现象可能是jdk版本太低 改为1.8即可
public class LogBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第三步:执行”Bean后处理器“的before方法");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第五步:执行”Bean后处理器“的after方法");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
需要在配置文件中进行配置
注意:这个Bean后处理器将作用于整个配置文件中所有的Bean
<bean class="com.bjpowernode.spring.bean.LogBeanPostProcessor">bean>
完整的:
<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 class="com.bjpowernode.spring.bean.LogBeanPostProcessor">bean>
<bean id="user" class="com.bjpowernode.spring.bean.User"
init-method="initBean" destroy-method="destroyBean">
<property name="name" value="小白">property>
bean>
beans>
比七步添加的三步在哪里?
实现BeanNameAware, BeanClassLoaderAware, BeanFactoryAware 接口
public class User implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware {
private String name;
public void setName(String name) {
System.out.println("第二步:给对象的属性赋值。");
this.name = name;
}
public User() {
System.out.println("第一步:无参数构造方法执行。");
}
public void initBean(){
System.out.println("第四步:初始化Bean。");
}
public void destroyBean(){
System.out.println("第七步:销毁Bean。");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("Bean这个类的加载器:"+classLoader);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("生产这个Bean的工厂对象是:"+beanFactory);
}
@Override
public void setBeanName(String name) {
System.out.println("这个Bean的名字是:"+name);
}
}
实现InitializingBean接口
public class User implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean {
public void setName(String name) {
System.out.println("第二步:给对象的属性赋值。");
}
public User() {
System.out.println("第一步:无参数构造方法执行。");
}
public void initBean(){
System.out.println("第四步:初始化Bean。");
}
public void destroyBean(){
System.out.println("第七步:销毁Bean。");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("Bean这个类的加载器:"+classLoader);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("生产这个Bean的工厂对象是:"+beanFactory);
}
@Override
public void setBeanName(String name) {
System.out.println("这个Bean的名字是:"+name);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean's afterPropertiesSet执行。");
}
}
实现DisposableBean接口
public class User implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean,DisposableBean {
public void setName(String name) {
System.out.println("第二步:给对象的属性赋值。");
}
public User() {
System.out.println("第一步:无参数构造方法执行。");
}
public void initBean(){
System.out.println("第四步:初始化Bean。");
}
public void destroyBean(){
System.out.println("第七步:销毁Bean。");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("Bean这个类的加载器:"+classLoader);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("生产这个Bean的工厂对象是:"+beanFactory);
}
@Override
public void setBeanName(String name) {
System.out.println("这个Bean的名字是:"+name);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean's afterPropertiesSet执行。");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean's destroy执行");
}
}