zoukankan      html  css  js  c++  java
  • 在spring配置文件中引入外部properties配置文件 context:property-placeholder

    在spring的配置文件中,有时我们需要注入很多属性值,这些属性全都写在spring的配置文件中的话,后期管理起来会非常麻烦。所以我们可以把某一类的属性抽取到一个外部配置文件中,使用时通用spring的EL表达式引入就可以了。这样可以方便我们管理属性。

    步骤:

    1.引入context名称空间,并使用  <context:property-placeholder location="外部配置文件"/>  引入外部配置文件

    <?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:component-scan base-package="com.xj"/>
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
    </beans>

    2.创建一个外部配置文件jdbc.properties,并设置键值对

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/userDb
    jdbc.userName=root
    jdbc.password=123456

    3.在spring配置文件中通过spEL表达式引入外部配置文件jdbc.properties中的值

    <?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:component-scan base-package="com.xj"/>
    
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.userName}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    </beans>
  • 相关阅读:
    SpringMVC 中整合之JSON、XML
    解决Django扩展用户表后新表内增加用户密码存储为明文
    python之路:始解(四)
    python作业:select版本FTP
    python作业:高级FTP程序
    python归档:笔记转化
    python之路:始解(三)
    python之路:始解(二)
    python作业:FTP程序
    python文件操作:pickle模块多次dump后出现的读取问题
  • 原文地址:https://www.cnblogs.com/bear7/p/13657470.html
Copyright © 2011-2022 走看看