2015-03-19 3 views
6

Momentan habe ich eine Spring-XML-Konfiguration (Spring 4), die eine Eigenschaftendatei lädt.Spring-Platzhalter löst keine Eigenschaften in JavaConfig auf

context.properties

my.app.service = myService 
my.app.other = ${my.app.service}/sample 

Frühling xml Konfiguration

<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="fileEncoding" value="UTF-8" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:context.properties</value> 
     </list> 
    </property> 
</bean> 
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="properties" ref="contextProperties" /> 
    <property name="nullValue" value="@null" /> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
</bean> 

Bean, die

@Component 
public class MyComponent { 

    @Value("${my.app.other}") 
    private String others; 

} 

Das funktioniert perfekt und der others Wert MyService/sample die Eigenschaften verwenden, da vorbehalten. Aber wenn ich versuche, diese Konfiguration durch eine JavaConfig zu ersetzen, funktioniert die @Value in meiner Komponente nicht auf die gleiche Weise. Der Wert ist nicht myService/sample sondern ${my.app.service}/sample.

@Configuration 
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"}) 
public class PropertiesConfiguration { 

    @Bean 
    public static PropertyPlaceholderConfigurer placeholder() throws IOException { 
     PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer(); 
     placeholder.setNullValue("@null"); 
     placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); 
     return placeholder; 
    } 

} 

Habe ich etwas in der Konvertierung von XML nach Javaconfig vermisst?

ps: Ich versuche auch eine PropertySourcesPlaceholderConfigurer statt einer PropertyPlaceholderConfigurer ohne Erfolg zu instanziieren.

+0

Auch wenn es Ihr Problem nicht löst, kann ich bestätigen, dass Sie 'PropertySourcesPlaceholderConfigurer', nicht' PropertyPlaceholderConfigurer' verwenden sollten – superbob

+0

Kann die 'my.app.service' -Eigenschaft ordnungsgemäß aufgelöst werden? Überprüfen Sie mit einem '@Value (" $ {my.app.service} ")', ich wandern, wenn das Problem von der Eigenschaftsverschachtelung kommt. – superbob

Antwort

2

Update zu verwenden konfigurieren PropertySourcesPlaceholderConfigurer. Nur @PropertySource Anmerkung, die nicht ausreichen werden:

@Bean 
public static PropertySourcesPlaceholderConfigurer properties() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

@PropertySource Annotation nicht automatisch ein PropertySourcesPlaceholderConfigurer mit Feder registrieren. Daher müssen wir ausdrücklich PropertySourcesPlaceholderConfigurer

Below JIRA Ticket weitere Informationen über die Gründe hinter diesem Entwurf konfigurieren hat:

https://jira.spring.io/browse/SPR-8539

UPDATE: Erstellt einfache Feder Boot-Anwendung verschachtelte Eigenschaften zu verwenden. Es funktioniert gut mit der obigen Konfiguration.

https://github.com/mgooty/property-configurer/tree/master/complete

+0

Aktualisiert die Antwort – Mithun

+0

Ich denke, das OP hat das bereits getan, und es hat nicht funktioniert, siehe seine "PS" in der Frage. Ich gehe umher, wenn das Problem mit der Eigenschaftsverschachtelung in Verbindung zu stehen scheint ('my.app.other = $ {my.app.service}/sample') – superbob

1

Eine weitere Option ist PropertyPlaceholderAutoConfiguration.class zu importieren.

import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; 

@Import(PropertyPlaceholderAutoConfiguration.class) 

Die Annotation enthält einen PropertySourcesPlaceholderConfigurer im Kontext, wenn dieser nicht vorhanden ist.

+0

Ja, das funktioniert für ** Spring-Boot ** -basierten Kontext. –