java - 在 Spring Boot 中使用 spring.application.json 作为 application.properties 文件

标签 java spring-boot

我正在尝试使用 spring 文档中的命令 -Dspring.application.json='{"foo":{"bar":"spam"}}' 但在看到它时IntelliJ 的运行命令总是失败,并显示 无法解析占位符

我尝试使用系统变量和 Java Ops 变量,但没有成功。

我的代码如下: 应用程序属性:

testing=${foo.bar}

应用程序.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 虚拟机选项: -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

我只是将配置放在错误的位置才能使其正常工作吗?

最佳答案

你不能像这样“扩展”Spring的application.properties。那里的所有值和系统级 spring.application.json 都会加载到您的 Environment 变量中,您可以访问 foo.bar从那里开始。

所以,基本上..

Spring 从您的 application.properties 中获取 testing,期望得到一个完全形成的值,然后从您的系统变量中获取 JSON,期望一个完全形成的值并将它们合并到环境中。

现在你可以做

@Autowired
private Environment env;

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

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

关于java - 在 Spring Boot 中使用 spring.application.json 作为 application.properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39723465/

相关文章:

java - GWT 网址参数

javascript - Spring Boot Controller - 将 Multipart 和 JSON 上传到 DTO

java - 无法从 START_OBJECT token 中反序列化 int[] 实例

spring-boot - Cors 过滤器不适用于 Spring Boot

spring-boot - Spring Boot + Keycloak 使用私有(private)证书

java - 无法从 servlet 访问 WEB-INF 文件夹中的文件

java正则表达式有时无法匹配

java - Selenium Webdriver Java : Element Not Visible Exception

java - 使用 Java EE 7 设置基本文档云

java - Spring boot/quartz webapp 不发布 "actuator/quartz"端点