在Spring Boot中将spring.application.json用于application.properties文件

舍弃

我正在尝试使用-Dspring.application.json='{"foo":{"bar":"spam"}}'spring docs中的命令,但是在IntelliJ的run命令中看到它时,它总是失败Could not resolve placeholder

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

我有什么代码明智的:Application.properties:

testing=${foo.bar}

应用程序

@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

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

罗夏

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

所以基本上

Spring会testing从你application.properties期待一个完全成形的价值,那么它需要的JSON从你的系统变量,期待一个完全成形的价值,将它们合并成Environment

现在你可以做

@Autowired
private Environment env;

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Spring Boot中将application.properties文件加载到java.util.Properties

Spring Boot jar中的application.properties文件在哪里?

从 spring boot application.properties 文件访问 amqp_URI

Heroku Spring application.properties文件

Spring Boot-用于定义HTTP请求的baseUrl的配置文件(application.properties)

在application.properties中用于Spring Boot的Keycloak配置

有什么方法可以在application.properties文件中写入JSON数组列表(spring-boot)

Intelij Spring Boot 2 application.properties

Spring Boot 从 application.properties 读取值

Spring Boot邮件忽略application.properties

Spring Boot的application.yml和application.properties

Spring application.properties文件中的布尔值?

Spring Redis-从application.properties文件读取配置

在Spring application.properties文件中使用表达式

spring-boot 配置文件:resources/profiles/application-*.properties

运行测试时如何指示Spring Boot在测试资源中使用application.properties文件

如何在Spring Boot中的application.properties中指定外部属性文件?

如何将Spring Boot中的application.properties文件外部化为例如外部依赖的JAR?

如何在Spring Boot应用程序的application.properties文件中设置MyBatis配置属性?

如何将Spring Boot中的URL外部化为application.properties文件?

如何在spring-boot中添加多个application.properties文件?

如何在我的application.properties文件的Spring Boot应用程序中配置HikariCP?

如何在Spring Boot中访问application.properties文件中定义的值

合并许多application.properties文件,而不是在Spring Boot上替换?

如何将Spring Boot application.properties外部化为tomcat / lib文件夹

具有application.properties的spring-boot数据源配置文件

Spring Boot application.properties扩展了另一个属性文件

使用Spring Boot在运行时以编程方式更改application.properties文件

在Spring Boot application.properties/.yml中基于OS设置日志文件位置