当前位置: 首页 > article >正文

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>


http://www.kler.cn/a/584428.html

相关文章:

  • Maven 构建 项目测试
  • Qt常用控件之垂直布局QVBoxLayout
  • Leetcode9-回文数
  • 解决:外部调用存储过程时突然变慢,但是在sql server运行很快
  • ChromeOS 134 版本更新
  • 专业视角:set 和 multiset的原理与应用解析
  • (2025|ICLR|厦大华为,LoSA,基于表示互信息的动态层级稀疏率,基于重构误差的秩分配)LLM 的动态低秩稀疏自适应
  • SQL Server数据库基于SQL性能优化
  • 迪威 3D 模型发布系统:制造业产品展示革新利器
  • 批量给 Excel 添加或删除密码保护|Excel 批量设置打开密码和只读密码
  • 【3dmax笔记】008:选择工具
  • 数字隔离器,如何提升储能系统的安全与效能?
  • k8s集群中部署dcgm-exporter收集GPU指标
  • 5-27 临摹大师-IP-Adapter
  • 【Docker项目实战】使用Docker与Caddy部署BanBan任务管理工具
  • 如何搭建一个适配微信小程序,h5,app的uni-app项目
  • C# NX二次开发:获取模型中所有的草图并获取草图中的对象
  • 基于SpringBoot + Vue 的校园论坛系统
  • K8S学习之基础二十六:k8s的StatefulSet控制器
  • 在 Ubuntu 上安装和配置 Docker 的完整指南