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>
  • 相关阅读:
    工具包分享-常用工具。by-某某
    渗透常用dos命令,http协议及数据提交方式。 hack 某某
    Hello This Cruel World!
    FFT的一个小技巧
    未完成的模板
    进制转换详细讲解
    CodeForces练习计划
    [SDOI2013]随机数生成器-题解
    动态dp模板
    noip2018游记
  • 原文地址:https://www.cnblogs.com/bear7/p/13657470.html
Copyright © 2011-2022 走看看