zoukankan      html  css  js  c++  java
  • Spring容器管理各种文件

    1. 导入文件

      <import resource="applicationContext-dataSource.xml" />

    2. 引用资源配置文件

    <context:property-placeholder location="classpath:jdbc.properties,classpath:xxx.properties"/>
    或者
    <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
    <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>

      多次引用没有ignore-unresolvable="true"一定会出"Could not resolve placeholder"异常。

      Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,所以就不能使用上面的那种方法去配置,

      可以改如下的格式:

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:/jdbc.properties</value>
    </list>
    </property>
    </bean>

      使用引用的文件内容:

    <bean name="userInfo" class="test.UserInfo">  
      <property name="username" value="${db.username}"/>  
      <property name="password" value="${db.password}"/>  
    </bean> 

    3. 在项目java代码中引用资源文件

      例如引用xxx.properties中的内容

      首先在spring容器中配置:

    <util:properties id="settings" location="/WEB-INF/xxx.properties"></util:properties>

      java代码中使用方法:

    @Value("#{settings}")   
    private Properties file;
    或者
    @Value("#{settings['test.abc']}")   
    private String url;
  • 相关阅读:
    牛客(14)链表中倒数第k个结点
    牛客(13)调整数组顺序使奇数位于偶数前面
    牛客(12)数值的整数次方
    牛客(11)二进制中1的个数
    牛客(10)矩形覆盖
    牛客(9)变态跳台阶
    牛客(8)跳台阶
    牛客(7)斐波那契数列
    Docker数据卷
    Docker镜像
  • 原文地址:https://www.cnblogs.com/cxyj/p/3885319.html
Copyright © 2011-2022 走看看