微服务 教程
1085个小节阅读:196.1k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Bean对象的生命周期包含创建——使用——销毁,Spring可以配置Bean对象在创建和销毁时自动执行的方法:
定义生命周期方法
xxxxxxxxxx
public class StudentDaoImpl2 implements StudentDao{
// 创建时自动执行的方法
public void init(){
System.out.println("创建StudentDao!!!");
}
// 销毁时自动执行的方法
public void destory(){
System.out.println("销毁StudentDao!!!");
}
}
配置生命周期方法
xxxxxxxxxx
<!-- init-method:创建对象时执行的方法 destroy-method:销毁对象时执行的方法 -->
<bean id="studentDao" class="com.itbaizhan.dao.StudentDaoImpl2" scope="singleton"
init-method="init" destroy-method="destory"></bean>
测试
xxxxxxxxxx
@Test
public void t3(){
// 创建Spring容器
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
// 销毁Spring容器,ClassPathXmlApplicationContext才有销毁容器的方法
ac.close();
}
1. 在Spring中,<bean>
中配置属性可以定义创建对象时执行的方法
A id
B method
C init-method
D destroy-method
2. 在Spring中,<bean>
中配置属性可以定义销毁对象时执行的方法
A id
B method
C init-method
D destroy-method
1=>C 2=>D