【问题标题】:Unable to load property file in spring context无法在弹簧上下文中加载属性文件
【发布时间】:2016-09-02 09:31:50
【问题描述】:

我无法将文件路径加载到我的应用程序中。任何人都可以帮助我。

这里是 spring xml 配置:

<context:property-placeholder location="file:${globalproplocation}"
        ignore-unresolvable="true" ignore-resource-not-found="true" order="-1" />

我已经在catalina.sh下的JAVA_OPTS中添加了这个如下图:

JAVA_OPTS="-XX:MaxPermSize=4096m  -XX:PermSize=1024m -Dglobalproplocation=/Users/admin/properties/temp.properties"

但是有些没有被捡起来,这是我的tomcat日志。

    <Loading properties file from ServletContext resource [/${globalproplocation}]>
    2016-05-07 02:52:24,089 WARN [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] -
<Could not load properties from ServletContext resource [/${globalproplocation}]: 
Could not open ServletContext resource [/${globalproplocation}]>

我在这里做错了什么?

【问题讨论】:

    标签: java spring tomcat7


    【解决方案1】:

    听到的问题是,当 Spring Context 启动并在您的配置文件中看到一个占位符 ${globalproplocation} 时,它会尝试加载如下文件路径:${globalproplocation}。听到的问题是,当您传递 -Dglobalproplocation=/Users/admin/properties/temp.properties 之类的参数时,xml 不会被修改,并且上下文会继续尝试加载一个不连续的文件。为了克服这个问题,您可以使用 maven 和资源处理插件,配置如下。

    <build>
        <properties>
                <globalproplocation>/Users/admin/properties/temp.properties</globalproplocation>
        </properties>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    
        <plugins>
            <!-- Facilitates downloading source and javadoc in Eclipse -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    然后你可以执行命令 mvn clean package 并且构建结果将有 xml 文件将被转换并且 palceholder 将被替换为正确的路径。

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      spring xml 配置:

      `<bean 
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="propname">
              <value>test.properties</value>
          </property>
      </bean>`
      

      备用选项使用类路径

      <bean name="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="propname"> <value>classpath:test.properties</value> </property> </bean>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-15
        • 2012-08-04
        • 2020-01-10
        • 1970-01-01
        • 2022-10-21
        • 1970-01-01
        • 2018-05-06
        相关资源
        最近更新 更多