您可以通过在 xml 中添加 context:property-placeholder 来解决此问题。这样你就可以告诉 Spring 加载你的特定属性文件。
然而,另一个更符合 Spring Boot 解决方案的方法是使用 application.properties 作为属性文件的名称,将其放在预期位置之一,并使用 @EnableAutoconfiguration 注释。
Spring Boot 期望 application.properties 按优先顺序位于以下位置。
- 当前目录的 /config 子目录。
- 当前目录
- 类路径/配置包
- 类路径根
我已经尝试过这个并且有效。
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());
}
}
示例服务.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>
应用程序属性
sample=hello
在输出中你会得到你好世界如果你运行该程序。
确保您已启用自动配置。如果不这样做,它将无法按预期工作。为此,请添加 @EnableAutoconfiguration 注释,如示例中所示。
请注意,您正在使用 Spring Boot,因此建议您避免 XML 配置。即使根本没有 beans.xml,您也可以获得相同的结果。不过,如果您仍然需要它,您可以将 XML 与注释混合使用。
我已将两个示例项目上传到GitHub,请查看。
https://github.com/plopcas/example-spring-boot/tree/master/spring-boot-xml https://github.com/plopcas/example-spring-boot/tree/master/spring-boot-xml
https://github.com/plopcas/example-spring-boot/tree/master/spring-boot https://github.com/plopcas/example-spring-boot/tree/master/spring-boot
希望这可以帮助。