您可以通过在XML中添加context:property-placeholder来解决该问题。这样,您将告诉Spring加载您的特定属性文件。
但是,另一个与Spring
Boot解决方案保持一致的方法是,仅将application.properties用作属性文件的名称,并将其放在预期的位置之一,并使用@EnableAutoconfiguration批注。
Spring Boot希望在以下位置按优先顺序排列application.properties。
- 当前目录的/ config子目录。
- 当前目录
- 类路径/ 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>
Sample.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
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 > <property name="field" value="${sample}-world"></property> </bean></beans>application.properties
sample=hello
如果您运行该程序,则在输出中将显示 hello world 。
确保已启用自动配置。 如果您没有这样做,它将无法按预期工作。为此,请像示例中一样添加@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
希望这可以帮助。



