美文网首页
006.资源文件properties的配置

006.资源文件properties的配置

作者: 胖先森 | 来源:发表于2017-05-17 13:49 被阅读0次

Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下:

<context:property-placeholder location="classpath:jdbc.properties"/>

如果想要配置多个properties文件

<context:property-placeholder location="classpath:jdbc.properties"/>

<context:property-placeholder location="classpath:jdbc.properties"/>

这种方式是不被允许的,一定会出"Could not resolve placeholder"。

解决方案:

1.Spring 3.0 解决方案

<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>

2.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>
        <value>classpath:shxt.properties</value>
      </list>
    </property>
</bean>

3.通配符 解决方案

<context:property-placeholder location="classpath*:conf/conf_*.properties"/>   

实际应用的写法

public abstract class BaseController {
    /**
     * 管理基础路径
     */
    @Value("${adminPath}")
    protected String adminPath;
    /**
     * 前端基础路径
     */
    @Value("${frontPath}")
    protected String frontPath;
    /**
     * 前端URL后缀
     */
    @Value("${urlSuffix}")
    protected String urlSuffix;
}

XML的配置信息

    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:jeesite.properties" />
    
    <!-- 加载应用属性实例,可通过  @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver 方式引用 -->
    <util:properties id="APP_PROP" location="classpath:jeesite.properties" local-override="true"/>

    <!-- 定义视图文件解析 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="${web.view.prefix}"/>
        <property name="suffix" value="${web.view.suffix}"/>
    </bean>

    <!-- 定义无Controller的path<->view直接映射 -->
    <mvc:view-controller path="/" view-name="redirect:${web.view.index}"/>

4.补充知识点

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
    <util:properties id="entProp" location="classpath:/entitlement.cfg" />
    
    <util:properties id="subProp" location="classpath:/subscription.cfg" />
    
</beans>

public class AppConfig  {
    private @Value("#{entProp['entitlement.name']}") String entName;
    private @Value("#{entProp['entitlement.time']}") int entTime;
    
    private @Value("#{subProp['subscription.name']}") String subName;
    private @Value("#{subProp['subscription.type']}") String subType;
}

相关文章

网友评论

      本文标题:006.资源文件properties的配置

      本文链接:https://www.haomeiwen.com/subject/eujdxxtx.html