2014-10-16 69 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