2014-10-16 64 views
7

春季启动 - 财产不能在XML从application.properties

@Configuration class加载XML配置使用@ImportResource("path/to/xml")弹簧启动应用程序,它包含以下行

<property name="bla" value="${log.directory}/file.ext" /> 

src/main/resources解决我有application.properties文件,内容如下:

log.directory=C:/path/I/Need 

然而,当我运行它未能按如下方式加载属性:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext

+0

你有一个属性占位符解析器bean吗? – 2014-10-16 14:32:28

+0

不应该Spring Boot自动加载application.properties? – mangusbrother 2014-10-16 14:33:58

+0

我不知道引导得很好。你是否期望从类路径中加载所有'.properties'? – 2014-10-16 14:34:52

回答

8

你可以解决它添加背景:物业占位符给你的XML。这样你会告诉Spring加载你的特定属性文件。

然而,另一个更符合Spring Boot解决方案的就是使用application.properties作为属性文件的名称,并将它放在其中一个预期位置,并使用@EnableAutoconfiguration注释。

Spring Boot期望application.properties位于以下位置,并按优先顺序排列。

  1. 当前目录的A/config子目录。
  2. 当前目录
  3. 类路径/配置包
  4. 类路径根

我已经试过这和它的作品。

的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 class="sample.SampleService"> 
     <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

希望这有助于。

+1

我已经有一个工作beans.xml配置,只需要使用spring-boot来执行此操作。问题是我需要添加 mangusbrother 2014-10-23 13:22:52

+0

好吧,是的,与上下文:property-placeholder it同样的作品。当你的属性文件被调用与application.properties不同的东西时,这是特别有用的。此外,当您有多个属性文件时,您的额外属性order =“1”ignore-unresolvable =“true”很有用。 – 2014-10-23 20:06:21

+0

但正如您在我的GitHub项目st-springboot中看到的那样,由于Spring Boot支持的配置约定,这不是必需的。但是,您也可以拥有一个beans.xml,并将注释与xml配置(st-springboot-xml)混合使用,并且假设您已启用自动配置(@EnableAutoconfiguration),Spring Boot将自动选择application.properties文件。我已经更新了答案以突出显示。无论如何,很高兴你解决了这个问题。如果你有时间,请检查项目,因为我认为这是对你的问题更具体的答案。问候。 – 2014-10-23 20:08:57