Bean的生命周期
初始化容器
- 创建对象(内存分配)
- 执行构造方法
- 执行属性注入(set 操作)
- 执行 bean 初始化方法
使用 bean
- 执行业务操作
关闭 / 销毁容器
- 执行 bean 销毁方法
提供生命周期控制方法:
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
// 初始化方法
public void init(){
System.out.println("book init ...");
}
// 销毁方法
public void destory(){
System.out.println("book destory ...");
}
}
配置生命周期控制:
<bean id="bookDao"
class="com.itheima.dao.impl.BookDaoImpl"
init-method="init"
destroy-method="destory"/>
另一种方法
实现类代码:
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
public void save() {
System.out.println("book service save ...");
}
// 初始化方法(InitializingBean接口方法)
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
// 销毁方法(DisposableBean接口方法)
public void destroy() throws Exception {
System.out.println("destroy");
}
}
bean销毁时机
- 触发时机:容器关闭前触发 bean 的销毁
- 关闭容器方式:
- 手工关闭容器:通过
ConfigurableApplicationContext接口的close()操作 - 注册关闭钩子:在虚拟机退出前先关闭容器,通过
ConfigurableApplicationContext接口的registerShutdownHook()操作
- 手工关闭容器:通过
- 代码示例:
public class AppForLifeCycle {
public static void main( String[] args ) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.close();
}
}
依赖注入方式
setter 注入 —— 引用类型
步骤 1:定义属性并提供 set 方法
public class BookServiceImpl implements BookService{
private BookDao bookDao;
// 提供set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
步骤 2:配置注入(property 标签 + ref 属性)
<!-- 配置依赖的bean -->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<!-- 配置目标bean并注入引用类型 -->
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/> <!-- name对应属性名,ref对应依赖bean的id -->
</bean>
setter 注入 —— 简单类型
步骤 1:定义属性并提供 set 方法
public class BookDaoImpl implements BookDao {
private int connectionNumber;
// 提供set方法
public void setConnectionNumber(int connectionNumber) {
this.connectionNumber = connectionNumber;
}
}
步骤 2:配置注入(property 标签 + value 属性)
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="connectionNumber" value="10"/> <!-- name对应属性名,value对应简单类型值 -->
</bean>
构造器注入 —— 引用类型(了解)
- 步骤 1:定义属性并提供构造方法
public class BookServiceImpl implements BookService{
private BookDao bookDao;
// 提供含引用类型参数的构造方法
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}
}
- 步骤 2:配置注入(constructor-arg 标签 + ref 属性)
<!-- 配置依赖的bean -->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<!-- 配置目标bean并构造器注入引用类型 -->
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/> <!-- name对应构造方法参数名,ref对应依赖bean的id -->
</bean>
构造器注入 —— 参数适配(了解)
- 按形参类型注入(type 属性)
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg type="int" value="10"/> <!-- 匹配int类型的形参 -->
<constructor-arg type="java.lang.String" value="mysql"/> <!-- 匹配String类型的形参 -->
</bean>
- 按形参位置注入(index 属性)
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg index="0" value="10"/> <!-- 匹配第1个形参(索引从0开始) -->
<constructor-arg index="1" value="mysql"/> <!-- 匹配第2个形参 -->
</bean>
依赖自动装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframeworkframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframeworkframework.org/schema/beans http://www.springframeworkframework.org/schema/beans/spring-beans.xsd">
<!-- 配置依赖的bookDao bean -->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<!-- 配置bookService并按名称自动装配(byName) -->
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byName"/>
<!-- 配置bookService并按类型自动装配(byType) -->
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
</beans>
- 自动装配仅用于引用类型依赖注入,不支持简单类型
- 按类型装配(byType):需保证容器中同类型 bean 唯一,推荐使用
- 按名称装配(byName):需保证容器中有指定名称的 bean,但因变量名与配置耦合,不推荐使用
- 自动装配优先级低于 setter 注入、构造器注入,同时存在时自动装配配置失效
1613

被折叠的 条评论
为什么被折叠?



