2016-09-27 26 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