Springbean的生命周期为bean对象从创建到销毁的过程,他有两种情况
一,不添加后置处理器的情况
1,构造器方法
2,set方法
3,初始化方法
4,实例化bean
5,销毁方法
代码示例
Student类
package awz;
/**
* bean的生命周期
* @version 1.0
* @Author:杨杰
* @Date:2022/4/6 8:53
*/
public class Student {
private String name;
public Student() {
System.out.println("1 调用无参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("2,调用set方法");
this.name = name;
}
public void initMethod() {
System.out.println("3,初始化方法");
}
public void destroyMethod() {
System.out.println("5,销毁方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
xml配置
<?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="student" class="awz.Student" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="yj"></property>
</bean>
</beans>
运行测试
public class Mytest {
@Test
public void testHello() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beanStudent.xml");
Student student = context.getBean("student", Student.class);
System.out.println("4,实例化student");
context.close();
}
}
二添加后置处理器的方法
1,构造器方法
2,set方法
在初始化之前执行
3,初始化方法
在初始化之后执行
4,实例化bean
5,销毁方法
需要注意的是第4步和第6步:把 bean 实例传递 bean 后置处理器的方法
需要传递bean示例
创建Students类实现BeanPostProcessor
package awz;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @version 1.0
* @Author:杨杰
* @Date:2022/4/6 9:03
*/
public class Students implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化方法之前调用");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化方法之后调用");
return bean;
}
}
xml配置
<?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="student" class="awz.Student" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="yj"></property>
</bean>
<bean id="students" class="awz.Students">
</bean>
</beans>
测试
import awz.Hello;
import awz.Student;
import awz.Students;
import awz.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @version 1.0
* @Author:杨杰
* @Date:2022/4/3 21:59
*/
public class Mytest {
@Test
public void testHello() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beanStudent.xml");
Students students = context.getBean("students", Students.class);
System.out.println("5,实例化students");
context.close();
}
}
#注:不能单独在注入Students必须和Student一起使用
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133788.html