Spring 3.0无法加载属性

时间:2012-05-16 22:52:10

标签: java spring apache java-ee netbeans

我在部署网络应用时遇到以下错误/异常:

  

org.springframework.beans.factory.BeanInitializationException:可以   不加载属性;嵌套异常是   java.io.FileNotFoundException:无法打开ServletContext资源   [/ WEB-INF / WebAppProps]

以下是我在 applicationContext.xml 中使用的上下文标记,指向 WebAppsProps.properties 文件

   <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="classpath:/WEB-INF/WebAppProps.properties" />

我也使用过:

  <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/WebAppProps.properties" />

该文件实际上是在文件系统和文件系统中。下面是我的项目结构的快照:

enter image description here

我也试过,将“WebAppProps.properties”放在类路径上并使用这两种变体

变体1:

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

变体2:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location"> 
            <value>WebAppProps.properties</value> 
        </property> 
    </bean> 

请参阅以下内容: enter image description here

但是我仍然得到相同的错误/异常。

请告知

谢谢

5 个答案:

答案 0 :(得分:4)

我越过相同的问题,

<强>修正:解决方案 你的bean如下:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
    <value>/WEB-INF/database.properties</value>
  </property>
</bean>

WEB-INF下创建文件夹属性,看起来像WEB-INF/properties/database.properties

因为类PropertyPlaceholderConfigurer默认搜索属性文件夹首先位于/WEB-INF/下,所以它将您的文件名连接为路径

答案 1 :(得分:2)

我认为您应该更改代码并添加类似的属性:

    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/jdbc.properties" />
</bean>

答案 2 :(得分:1)

Spring 4这就是我如何配置属性文件

@Configuration
@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource(value = "${application.properties}", ignoreResourceNotFound = false)
})
public class WebServiceConfig {
---
}

在setenv.sh中启动我的tomcat时,我将路径定义为

-Dapplication.properties=file:location-of-file/application.properties"

通知文件:前缀在这里。这很重要。

现在,您可以将文件拖放到文件系统的任何位置,并在不更改代码的情况下更改路径

答案 3 :(得分:0)

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location">
        <value>WebAppProps.properties</value>
    </property>
</bean>

只需将文件放在classes目录的根目录中的类路径中即可。

答案 4 :(得分:0)

你有两个选择:

如果您不需要获取bean,只需要获取属性:

<context:property-placeholder location="/WEB-INF/WebAppProps.properties" file-encoding="UTF-8"/>

或者如果你需要bean(你可能会想要注入你的属性bean,如果你需要阅读很多道具......)

<bean id="configProperty" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
               <value>/WEB-INF/config.properties</value>
               <value>/WEB-INF/setup.properties</value>
           </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>