Spring中的配置文件参数化与类型转换器实现详解
配置文件参数化
把Spring配置文件中需要经常修改的字符串信息,转移到一个更小的配置文件中
1.Spring的配置文件中存在需要经常修改的字符串吗?
存在 以数据库连接相关的参数为代表
2.经常变化字符串,在Spring配置文件中,直接修改
不利于项目维护(修改)
配置文件参数化开发步骤
1.创建db.properties
//db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/sky_take_out?useSSL=false jdbc.username=root jdbc.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: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 http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <bean id="connection" class="factoryBean.ConnectionFactoryBean"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="password" value="${jdbc.password}"></property> <property name="username" value="${jdbc.username}"></property> </bean> </beans>
classPath类路径
自定义类型转换器
作用:Spring通过类型转换器把配置文件中字符串类型的数据,转换成了对象中成员变量对应的类型
自定义类型转换器
原因:当Spring内部没有提供特定类型转换器时,而程序员在应用过程中还需要使用,程序员自己开发自定义类型转换器。
- 类 implements Converter接口
public class MyDateConverter implements Converter<String, Date> {
/*
* 实现Converter接口,重写convert方法
* @param source
* @return
*/
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
- Spring配置文件中进行配置
MyDateConverter对象创建出来
<bean id="myDateConverter" class="xxx.MyDateConverter"/>
类型转换器的注册
目的:告知Spring框架,我们创建的MyDateConverter是一个类型转换器
<bean id="myDateConverter" class="converter.MyDateConverter">
<property name="pattern" value="yyyy-MM-dd"></property>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="myDateConverter"></ref>
</set>
</property>
</bean>
细节
- MydateConverter中的日期格式,通过依赖注入的方式,由配置文件完成赋值
package converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDateConverter implements Converter<String, Date> {
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
private String pattern;
/*
* 实现Converter接口,重写convert方法
* @param source
* @return
*/
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
- ConversionServiceFactoryBean定义id属性 值必须是conversionService
- Spring内置日期类型的转换器
日期格式:只支持2020/05/01
后置处理Bean
BeanPostProcessor作用:对Spring工厂所创建的对象,进行再加工
BeanPostProcessor运行原理分析
程序员实现BeanPostProcessor规定接口的方法
Object postProcessorBeforeInitiallization(Object bean,String beanName)
作用:Spring创建完对象,并进行注入后,可以运行before方法进行加工
获得Spring创建好的对象,通过方法的参数
最终返回值交给Spring框架
Object postProcessorAfterInitiallization(Object bean,String beanName)
作用:Spring创建完对象,并进行注入后,可以运行After方法进行加工
获得Spring创建好的对象,通过方法的参数
最终返回值交给Spring框架
实战中
很少处理Spring的初始化操作,没有必要区分Before After,著需要实现其中一个After方法即可
注意:
return Bean
BeanPostProcessor卡法步骤
- 类实现 BeanPostProcessor接口
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("postProcessBeforeInitialization");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
Category category;
if(bean instanceof Category){
category = (Category) bean;
category.setName("orange");
}
return bean;
}
- Spring配置文件进行配置
<bean id="myBeanPostProcessor" class="beanPost.MyBeanPostProcessor"></bean>