Spring-工厂高级特性
第十章、对象的生命周期
1、生命是对象的生命周期
指的是一个对象创建、存活、消亡的一个完整过程
2、为什么要学习对象的生命周期
由Spring负责对象的创建、存活、销毁,了解⽣命周期,有利于我们使⽤好Spring为我们创建的对象
3、生命周期的3个阶段
-
创建阶段
-
scope=“singleton”
Spring⼯⼚创建的同时,对象的创建 注意:设置scope=singleton 这种情况下 也需要在获取对象的同时,创建对象 <bean lazy-init="true"/>
-
scope=“prototype”
Spring⼯⼚会在获取对象的同时,创建对象 ctx.getBean("")
-
-
初始化阶段
Spring⼯⼚在创建完对象后,调⽤对象的初始化⽅法,完成对应的初始化操作
1. 初始化⽅法提供:程序员根据需求,提供初始化⽅法,最终完成初始化操作
2. 初始化⽅法调⽤:Spring⼯⼚进⾏调⽤
InitializingBean接⼝
//程序员根据需求,实现的⽅法,完成初始化操作
public void afterProperitesSet(){
}
对象中提供⼀个普通的⽅法
public void myInit(){
}
<bean id="product" class="xxx.Product" init-method="myInit"/>
-
细节分析
- 如果⼀个对象即实现InitializingBean 同时⼜提供的 普通的初始化⽅法 顺序
1. InitializingBean 2. 普通初始化⽅法
-
注⼊⼀定发⽣在初始化操作的前⾯
-
什么叫做初始化操作
资源的初始化:数据库 IO ⽹络 .....
- 销毁阶段
Spring销毁对象前,会调⽤对象的销毁⽅法,完成销毁操作
1. Spring什么时候销毁所创建的对象?
ctx.close();
2. 销毁⽅法:程序员根据⾃⼰的需求,定义销毁⽅法,完成销毁操作
调⽤:Spring⼯⼚完成调⽤
DisposableBean
public void destroy()throws Exception{
}
定义⼀个普通的销毁⽅法
public void myDestroy()throws Exception{
}
<bean id="" class="" init-method="" destroymethod="myDestroy"/>
- 细节分析
-
销毁⽅法的操作只适⽤于 scope=“singleton”
-
什么叫做销毁操作
主要指的就是 资源的释放操作 io.close() connection.close();
第十一章、配置文件参数化
把Spring配置⽂件中需要经常修改的字符串信息,转移到⼀个更⼩的配置⽂件中
1. Spring的配置⽂件中存在需要经常修改的字符串?
存在 以数据库连接相关的参数 代表
2. 经常变化字符串,在Spring的配置⽂件中,直接修改
不利于项⽬维护(修改)
3. 转移到⼀个⼩的配置⽂件(.properties)
利于维护(修改)
配置⽂件参数化:利于Spring配置⽂件的维护(修改)
1.配置文件参数的开发步骤
- 提供⼀个⼩的配置⽂件(.properities)
名字:随便
放置位置:随便
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/hezb
mysql.username=root
mysql.password=123456
- Spring的配置⽂件与⼩配置⽂件进⾏整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:/dataSource.properties"/>
<bean id="conn" class="com.xiaohe.factoryBean.ConnectionFactoryBean">
<property name="driverClassName" value="${mysql.driverClassName}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
</beans>
第十二章、自定义类型转换器
1、类型转换器
# 作⽤:
Spring通过类型转换器把配置⽂件中字符串类型的数据,转换成了对象中成员变量对应类型的数据,进⽽完成了注⼊
2、自定义类型转换器
# 原因:
当Spring内部没有提供特定类型转换器时,⽽程序员在应⽤的过程中还需要使⽤,那么就需要程序员⾃⼰定义类型转换器
3、自定义类型转换器编码
- 1、实现Converter接口
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(source);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return date;
}
}
-
2、Spring文件进行注册
- DateConverter对象创建
<bean id="dateConverter" class="com.xiaohe.converter.DateConverter"/>
- 类型转换器的注册
目的:告知Spring框架,我们所创建的DateConverter是一个类型转换器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:/dataSource.properties"/> <!--1、类型转换器对象的创建--> <bean id="dateConverter" class="com.xiaohe.converter.DateConverter"/> <!--2、注册类型转换器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="dateConverter"/> </set> </property> </bean> <bean id="person" class="com.xiaohe.converter.Person"> <property name="name" value="zhangsan"/> <property name="birthday" value="2000-10-12"/> </bean> </beans>
4、 细节
- DateConver中的日期格式,通过依赖注入,配置到配置文件中
public class DateConverter implements Converter<String, Date> {
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public Date convert(String source) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
date = sdf.parse(source);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return date;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:/dataSource.properties"/>
<!--1、类型转换器对象的创建-->
<bean id="dateConverter" class="com.xiaohe.converter.DateConverter">
<!--2、依赖注入-->
<property name="pattern" value="yyyy-MM-dd"/>
</bean>
<!--2、注册类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConverter"/>
</set>
</property>
</bean>
<bean id="person" class="com.xiaohe.converter.Person">
<property name="name" value="zhangsan"/>
<property name="birthday" value="2000-10-12"/>
</bean>
</beans>
- ConversionServiceFactoryBean的id必须为
conversionService
,注意大小写 - Spring框架内置了日期转换器
日期格式 2022/12/06 (2022-12-06 这种格式是不支持的)
第三章、后置处理Bean
# BeanPostProcessor作用:对Spring工厂所创建的对象进行再加工
# 注意:BeanPostProcessor接口
xxx(){
}
# 程序员实现BeanPostProcessor规定接⼝中的⽅法:
Object postProcessBeforeInitiallization(Object bean String beanName)
作⽤:Spring创建完对象,并进⾏注⼊后,可以运⾏Before⽅法进⾏加⼯
获得Spring创建好的对象 :通过⽅法的参数
最终通过返回值交给Spring框架
Object postProcessAfterInitiallization(Object bean String beanName)
作⽤:Spring执⾏完对象的初始化操作后,可以运⾏After⽅法进⾏加⼯
获得Spring创建好的对象 :通过⽅法的参数
最终通过返回值交给Spring框架
# 实战中:
很少处理Spring的初始化操作:没有必要区分Before After。只需要实现其中的⼀个After
⽅法即可
注意:
postProcessBeforeInitiallization
return bean对象
-
BeanPostProcessor的开发步骤
- 1、类实现BeanPostProcessor接口
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof User){ User user = (User) bean; user.setName("xiaojr"); user.setPassword("666"); return user; } return bean; } }
- 2、Spring的配置⽂件中进⾏配置
<bean id="myBeanProcessor" class="com.xiaohe.BeanPostProcessor.MyBeanPostProcessor"/> <bean id="user" class="com.xiaohe.BeanPostProcessor.User"> <property name="name" value="zhangsan"/> <property name="password" value="123456"/> </bean>
- BeanPostProcessor细节
BeanPostProcessor会对Spring⼯⼚中所有创建的对象进⾏加⼯