Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 属性未在SpringXML中计算_Java_Spring_Spring Mvc - Fatal编程技术网

Java 属性未在SpringXML中计算

Java 属性未在SpringXML中计算,java,spring,spring-mvc,Java,Spring,Spring Mvc,我想使用property placeholder+spring.profiles.active处理多个环境。我将我的spring.profiles.active设置设置为VM参数,我的spring XML如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X

我想使用property placeholder+spring.profiles.active处理多个环境。我将我的spring.profiles.active设置设置为VM参数,我的spring XML如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:rcwkstn-automation-framework${spring.profiles.active}.properties</value>
        </list>
    </property>
</bean>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:rcwkstn-automation-framework.xml"})
问题是,当我运行代码时,我得到:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [rcwkstn-automation-framework${spring.profiles.active}.properties] cannot be opened because it does not exist
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
    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.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 25 more
Caused by: java.io.FileNotFoundException: class path resource [rcwkstn-automation-framework${spring.profiles.active}.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
    ... 34 more

看起来${spring.profiles.active}没有被计算并作为文本处理。有人知道这里可能有什么问题吗?

bean
PropertyPlaceHolderConfigure
本身用于指定需要加载的属性文件,以便从中解析占位符。所以很明显,如果你给了一个属性占位符,它将不会被加载,因为bean还没有创建,属性还没有加载

您需要提供属性文件的全名,否则,将所有属性文件移动到一个文件夹中,并通过提供
*.properties
(加载所有属性文件)加载该文件夹中的所有内容

例如:

classpath:config.properties//完整文件名
文件://${CATALINA_HOME}/prop文件夹//*。属性//加载所有道具

bean
PropertyPlaceHolderConfigure
本身用于指定需要加载的属性文件,以便从中解析占位符。所以很明显,如果你给了一个属性占位符,它将不会被加载,因为bean还没有创建,属性还没有加载

您需要提供属性文件的全名,否则,将所有属性文件移动到一个文件夹中,并通过提供
*.properties
(加载所有属性文件)加载该文件夹中的所有内容

例如:

classpath:config.properties//完整文件名
文件://${CATALINA_HOME}/prop文件夹//*。属性//加载所有道具

如何加载此配置文件?你能发布完整的stacktrace吗?另外,我没有直接指定bean,而是建议使用名称空间
,我还尝试了您提到的方法,得到了相同的结果,您是如何传递属性的?确保该属性作为a-D属性传递。替换仅适用于系统属性或环境变量。如何加载此配置文件?你能发布完整的stacktrace吗?另外,我没有直接指定bean,而是建议使用名称空间
,我还尝试了您提到的方法,得到了相同的结果,您是如何传递属性的?确保该属性作为a-D属性传递。替换只适用于系统属性或环境变量。应该可以,我们也可以做类似的事情(使用环境属性加载环境属性文件)。就像一个咒语,但它只适用于系统属性(-D)或环境属性。
config.properties
我指定的是一个用户定义的属性文件,甚至第二个示例也将加载该文件夹中的所有用户定义的属性文件。正确(相信我,我知道spring资源加载的工作原理)。但如前所述,它与占位符完美配合。我们正在向占位符传递这样的
文件:${CONFIG_HOME}/${APP_code}/${ENV_code}/
,没有任何问题。其中,
CONFIG_HOME
是一个环境变量,所有其他变量都是在运行时确定的。@M.Deinum-哦,我不知道你在说这个。这只是我举的一个例子。是的,你是对的。它应该可以工作,我们也可以做一些类似的事情(使用一个环境属性来加载环境属性文件)。就像一个咒语,但它只适用于系统属性(-D)或环境属性。
config.properties
我指定的是一个用户定义的属性文件,甚至第二个示例也将加载该文件夹中的所有用户定义的属性文件。正确(相信我,我知道spring资源加载的工作原理)。但如前所述,它与占位符完美配合。我们正在向占位符传递这样的
文件:${CONFIG_HOME}/${APP_code}/${ENV_code}/
,没有任何问题。其中,
CONFIG_HOME
是一个环境变量,所有其他变量都是在运行时确定的。@M.Deinum-哦,我不知道你在说这个。这只是我举的一个例子。是的,你说得对。
<value>classpath:config.properties</value> // full file name 
<value>file:///${CATALINA_HOME}/prop-folder/*.properties</value> // load all props