Spring Boot - 无法在 application.properties 的 xml 中解析属性 [英] Spring Boot - property could not be resolved in xml from application.properties
问题描述
我有一个 Spring Boot 应用程序
I have a spring boot application
My @Configuration class
使用 @ImportResource("path/to/xml")
加载 xml 配置,其中包含以下行
My @Configuration class
loads the xml Configuration using @ImportResource("path/to/xml")
, which contains the following line
<property name="bla" value="${log.directory}/file.ext" />
在 src/main/resources
下,我有 application.properties
文件,其内容如下:
Under src/main/resources
I have the application.properties
file with the following content:
log.directory=C:/path/I/Need
但是,当我运行时它无法加载属性,如下所示:
However when I run It fails to load the property as follows:
Caused by: java.lang.IllegalArgumentException: 无法解析字符串值${log.directory}/file.ext
"中的占位符log.directory"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext
"
推荐答案
您可以通过向您的 xml 添加 context:property-placeholder 来解决它.这样你就会告诉 Spring 加载你的特定属性文件.
You can solve it adding context:property-placeholder to you xml. That way you will tell Spring to load your specific properties file.
然而,另一个更符合 Spring Boot 解决方案的方法是使用 application.properties 作为属性文件的名称,将其放在预期位置之一,并使用 @EnableAutoconfiguration 注释.
However another more in line with Spring Boot solution is just using application.properties as the name for your properties file, having it in one of the expected locations, and use the @EnableAutoconfiguration annotation.
Spring Boot 期望 application.properties 按优先顺序位于以下位置.
Spring Boot expects the application.properties in the following location in order of preference.
- 当前目录的/config 子目录.
- 当前目录
- 类路径/config 包
- 类路径根
我已经试过了,它有效.
I have tried this and it works.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Sample</name>
<description>Spring Boot sample</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
示例.java
package sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:beans.xml")
public class Sample implements CommandLineRunner {
@Value("${sample}")
private String sample;
@Autowired
SampleService service;
public static void main(String[] args) {
SpringApplication.run(Sample.class, args);
}
public void run(String... args) {
System.out.println(service.greetings());
}
}
SampleService.java
SampleService.java
package sample;
public class SampleService {
private String field;
public String greetings() {
return field;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean class="sample.SampleService">
<property name="field" value="${sample}-world"></property>
</bean>
</beans>
application.properties
application.properties
sample=hello
在输出中,如果您运行该程序,您将得到 hello world.
In the output you will get hello world if you run the program.
确保您已启用自动配置.如果你没有,它不会按预期工作.为此,请添加@EnableAutoconfiguration 注释,如示例中所示.
请注意,您使用的是 Spring Boot,因此建议您避免使用 XML 配置.您可以在根本没有 beans.xml 的情况下获得相同的结果.不过,如果您仍然需要它,您可以将 XML 与注释混合使用.
Please note that you are using Spring Boot, so you are encouraged to avoid XML configuration. You can get the same result with no beans.xml at all. Although, if you still need it, you can mix XML with annotations.
我已将两个示例项目上传到 GitHub,请检查.
I have uploaded both sample projects to GitHub, please check.
https://github.com/plopcas/example-spring-boot/tree/master/spring-boot-xml
https://github.com/plopcas/example-spring-boot/tree/master/spring-boot
希望这会有所帮助.
这篇关于Spring Boot - 无法在 application.properties 的 xml 中解析属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!