man*_*her 8 java xml spring properties-file spring-boot
我有一个春季启动应用程序
我@Configuration class加载了xml Configuration using @ImportResource("path/to/xml"),其中包含以下行
<property name="bla" value="${log.directory}/file.ext" />
Run Code Online (Sandbox Code Playgroud)
在src/main/resources我有application.properties以下内容的文件:
log.directory=C:/path/I/Need
Run Code Online (Sandbox Code Playgroud)
但是当我运行时它无法加载属性,如下所示:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext"
Ped*_*pez 10
您可以解决它向您添加context:property-placeholder xml.这样你就会告诉Spring加载你的特定属性文件.
然而,另一个更符合Spring Boot解决方案的是使用application.properties作为属性文件的名称,将其置于其中一个预期位置,并使用@EnableAutoconfiguration批注.
Spring Boot期望application.properties按以下顺序排列在以下位置.
我试过这个并且它有效.
的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>
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
application.properties
sample=hello
Run Code Online (Sandbox Code Playgroud)
在输出中,如果运行程序,您将获得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
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
19399 次 |
| 最近记录: |