Spring environment cannot resolve placeholders

时间:2016-04-04 18:54:22

标签: java spring properties

Using spring 3.2.5. I have a web project with two application context (one parent of the other).

I configured, in xml, a placeholder configurer like this :

<context:property-placeholder location="WEB-INF/properties/*.properties, classpath:local.config.properties, file:${global.config.file}"
                              ignore-unresolvable="true"
                              ignore-resource-not-found="false"/>

The variable "global.config.file" is a path to a file specified in the JVM arguments passed to start tomcat.

In my code, I can successfully inject values from those files like this :

@Value("${blabla}")
private String blabla;

However, when I try to resolve place holders programmatically, via spring's Environment object, placeholders are not replaced with real values :

@Inject
Environment env;
// ...

public void test() {
  String blabla = env.resolvePlaceholders("${blabla}");
}

The blabla variables will contain it's unresolved value : "${blabla}" Also, doing :

env.getProperty("blabla");

returns null. Note that I'm using context schema location above 3.0 :

http://www.springframework.org/schema/context/spring-context-3.2.xsd

Can someone help me understand what I'm missing ? Or any clue on how to debug further ?

1 个答案:

答案 0 :(得分:0)

尝试使用classpath*:,例如:

<context:property-placeholder location="classpath*:WEB-INF/properties/*.properties, classpath*:local.config.properties, file:${global.config.file}"
                          ignore-unresolvable="true"
                          ignore-resource-not-found="false"/>