0

我敢肯定这已经被问了 1000 次,因为我见过他们,但是我错过了一些东西。

语境:

<beans profile="localDev">
    <util:properties id="propertiesLocalDev"location="classpath:/localDev.properties"/>
</beans>

<beans profile="test">
    <util:properties id="properties-test" location="classpath:/test.properties"/>
</beans>

在里面:

    System.setProperty("spring.profiles.active", "localDev");
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.load("classpath:applicationContext.xml");
    ctx.refresh();

配置:

@Configuration
public class AppConfig {


@Value("${test.value}")
private String testValue;
...

日志:

INFO: Loading properties file from class path resource [localDev.properties]

特性:

test.value=ugh

因此,似乎正在读取属性,但是未设置 AppConfig.testValue 中的值。我已经尝试过纯 java java/xml 等...一些配置破坏了一些工作,尝试使用@PropertySource,但常量是 testValue 永远不会被设置,所以我从根本上做错了什么。

总体目标是根据不同的配置文件加载不同的属性文件。

谁能看到我做错了什么?谢谢

4

3 回答 3

0

您还需要一个可以为您解析属性的PropertySourcesPlaceholderConfigurer 。这是使用以下配置的:

<context:property-placeholder location="..." 
                local-override="true" properties-ref="propertiesLocalDev" />

有了这个,您的财产价值应该干净地解决。

这也应该有效 - 使用 Spring-EL:

@Value("#{@propertiesLocalDev['test.value']}")
private String testValue;
于 2013-04-16T13:44:44.730 回答
0

尝试

public class AppConfig {

 @Autowired
private String testValue;

}

如果变量已正确自动装配,您可以使用

String sUgh = testValue.getProperty("test.value"); // = "ugh"

我也会用普通的

<util:properties id="propertiesLocalDev"location="classpath:/localDev.properties"/>

而不是使用

<beans profile> 

标签。

于 2013-04-16T13:46:02.537 回答
0

你必须使用类似的东西,在 XML 中只使用以下

<util:properties id="test" location="classpath:fn-test-configuration.properties" />

现在,您可以按照以下方式在类中使用属​​性值

@Value("#{test.test.value}")
私有字符串 testValue;

我用过同样的方法,它工作正常。

于 2013-04-16T14:06:32.223 回答