2016-09-27 332 views
1

我試圖從春天文檔使用命令-Dspring.application.json='{"foo":{"bar":"spam"}}'但是當看到它在的IntelliJ的運行命令它總是失敗,Could not resolve placeholder使用spring.application.json爲application.properties文件中的春天開機

我曾嘗試使用系統變量和Java Ops變量沒有成功。

我有什麼明智代碼: Application.properties:

testing=${foo.bar} 

Application.java

@SpringBootApplication 
@ComponentScan 
public class Application extends RepositoryRestMvcConfiguration { 

    @Value("${testing:}") 
    private String input; 

    public static void main(final String args[]) { 
     SpringApplication.run(Application.class, args); 
    } 

    @Bean 
    public BatchDetails set() { 
     System.out.println("input: " + input); 
     return new BatchDetails("Test", "Test2"); 
    } 
} 

的IntelliJ VM選項: -Dspring.application.json='{"foo":{"bar":"spam"}}'

的IntelliJ環境變量: SPRING_APPLICATION_JSON = '{"foo":{"bar":"spam"}}'

上的應用程序,我得到以下堆棧跟蹤的啓動:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'foo.bar' in string value "${foo.bar}" 
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver.resolveNestedPlaceholders(AbstractPropertyResolver.java:195) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:87) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:60) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:531) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:129) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:70) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:207) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
... 80 common frames omitted 

我只是把配置在錯誤的地方這個工作?

回答

1

你不能像這樣擴大Spring的application.properties。系統級別的所有值和系統級別spring.application.json都會加載到您的Environment變量中,您可以從那裏訪問foo.bar

所以,基本上..

春天從application.properties期待完全形成值取testing,那麼它需要的JSON從你的系統變量,期待完全形成值並將它們合併成Environment。現在

你可以做

@Autowired 
private Environment env; 

env.getProperty("testing"); 
env.getProperty("foo.bar"); 

//OR 
@Value(${"foo.bar"}) 
private String valueFromFooBar 
+0

所以我嘗試了'@ Value',它仍然不喜歡它。我真的想在本地模擬PCF的VCAP_SERVICES,所以我不想使用環境。我將在本地使用-Dfull.name.of.service =值。 – sherring