• spring引入外部属性文件


    spring引入外部属性文件

    有时候我们并不想把所有的配置信息都放到spring的配置文件中,这样的话也不太好维护,比如说数据库的信息

    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        bean>
    • 1

    这样配置当然也可以,只是要去修改spring配置文件的时候由于spring的配置文件内容太多,不太方便,所以有时候会引入外部的属性文件

    可以使用标签来进行引入

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >

      
      <context:property-placeholder location="classpath:db.properties"/>
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        bean>
    • 1

    db.properties属性文件

    url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    username=root
    password=123456
    
    • 1
    • 2
    • 3
    • 4

    会生成一个PropertySourcesPlaceholderConfigurer类来解析占位符

    https://zhhll.icu/2021/框架/spring/基础/15.spring引入外部属性文件/

    本文由 mdnice 多平台发布

  • 相关阅读:
    Servlet简单项目操作
    ArmSom---I2C开发指南
    【笔试刷题训练】day_14
    从零开始学正则(上)
    单源最短路径 dijkstra
    flutterdart chacha20加密
    在Linux上实现ECAT主站
    Nhanes临床数据库挖掘教程2—基线表绘制(table1)
    作为一名python开发者,想要兼职接单,需要学那些技术?要达到什么水准?为什么要学这些技术?
    22/6/24~6/25
  • 原文地址:https://blog.csdn.net/Lxn2zh/article/details/138188138