####先来看以下的图(Bean的一生)
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/5c2bed1073c78fe1480f58038b4bab66.png)
####可以简述为以下九步
实例化bean对象(通过构造方法或者工厂方法)
设置对象属性(setter等)(依赖注入)
如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。(和下面的一条均属于检查Aware接口)
如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身
将Bean实例传递给Bean的前置处理器的postProcessBeforeInitialization(Object bean, String beanname)方法
调用Bean的初始化方法
将Bean实例传递给Bean的后置处理器的postProcessAfterInitialization(Object bean, String beanname)方法
使用Bean
容器关闭之前,调用Bean的销毁方法
####先看一个最简单的一生(没有使用Bean的后置处理器)
package com.linjie.cycle;
import org.springframework.beans.factory.BeanNameAware;
/**
* @author LinJie
* @Description:一个学生类(Bean),能体现其生命周期的Bean
*/
public class Student implements BeanNameAware {
private String name;
//无参构造方法
public Student() {
super();
}
/** 设置对象属性
* @param name the name to set
*/
public void setName(String name) {
System.out.println("设置对象属性setName()..");
this.name = name;
}
//Bean的初始化方法
public void initStudent() {
System.out.println("Student这个Bean:初始化");
}
//Bean的销毁方法
public void destroyStudent() {
System.out.println("Student这个Bean:销毁");
}
//Bean的使用
public void play() {
System.out.println("Student这个Bean:使用");
}
/* 重写toString
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [name = " + name + "]";
}
//调用BeanNameAware的setBeanName()
//传递Bean的ID。
@Override
public void setBeanName(String name) {
System.out.println("调用BeanNameAware的setBeanName()..." );
}
}
1
package com.linjie.cycle;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* @author LinJie
*
*/
public class CycleTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
//Bean的使用
student.play();
System.out.println(student);
//关闭容器
((AbstractApplicationContext) context).close();
}
}
<?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">
<!-- init-method:指定初始化的方法
destroy-method:指定销毁的方法 -->
<bean id="student" class="com.linjie.cycle.Student" init-method="initStudent" destroy-method="destroyStudent">
<property name="name" value="LINJIE"></property>
</bean>
</beans>
1
---------------------
<?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">
<!-- init-method:指定初始化的方法
destroy-method:指定销毁的方法 -->
<bean id="student" class="com.linjie.cycle.Student" init-method="initStudent" destroy-method="destroyStudent">
<property name="name" value="LINJIE"></property>
</bean>
</beans>
1
---------------------
作者:浅然_
来源:CSDN
原文:https://blog.csdn.net/w_linux/article/details/80086950
版权声明:本文为博主原创文章,转载请附上博文链接!