有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Spring应用程序中加载内部(类路径)和外部属性文件

我需要在spring应用程序中加载外部和内部属性文件。一旦我声明外部文件如下

<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />

我可以访问此外部文件中定义的属性。但无法解析与我的类路径中的属性文件相关的所有属性

我的应用程序上下文

** <!--Refer External File --> **
<context:property-placeholder location="file:${JBOSS_HOME}/123.properties" />

 <!--Refer Internal File -->

<bean id="helloWorldBean"
    class="com.javacodegeeks.snippets.enterprise.services.HelloWorld">
    <property name="internalProperty1" value="${internalProperty1}" /> 

    <property name="**externalProperty**" value="${**externalProperty**}" />
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>constants.properties</value>
    </property>
</bean>

我得到的是外部属性文件的属性值,而不是内部属性文件的值

线程“main”中出现异常

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'helloWorldBean' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'internalProperty1' in string value "${internalProperty1}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplsamicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.javacodegeeks.snippets.enterprise.App.main(App.java:13)

我不能同时加载外部(非类路径)和内部(类路径)属性文件吗


共 (1) 个答案

  1. # 1 楼答案

    你需要的是这样的东西:

        <! Order matters, properties in the second file will override the first  >
    <context:property-placeholder location="file:${JBOSS_HOME}/123.properties,classpath:configuration.properties"
    ignore-unresolvable="false" ignore-resource-not-found="true" />