Le fichier Spring @Configuration avec le bean PropertyPlaceholderConfigurer ne résout pas l'annotation @Value

J'ai fichier de configuration suivant:

@Configuration
public class PropertyPlaceholderConfigurerConfig {

    @Value("${property:defaultValue}")
    private String property;

    @Bean
    public static PropertyPlaceholderConfigurer ppc() throws IOException {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocations(new ClassPathResource("properties/" + property + ".properties"));
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }
}

Je lance mon application avec la VM option:

-Dproperty=propertyValue

Donc je voudrais que mon application à charger propriété spécifique de fichiers au démarrage. Mais pour une raison quelconque, à ce stade, @Value les annotations ne sont pas traitées et la propriété est null. D'autre part, si j'ai PropertyPlaceholderConfigurer configuré via le fichier xml - tout fonctionne parfaitement comme prévu. Fichier Xml exemple:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="location">
        <value>classpath:properties/${property:defaultValue}.properties</value>
    </property>
</bean>

Si j'essaie d'injecter la valeur de la propriété dans un autre fichier de configuration Spring - il est bien injecté. Si je déplace mon PropertyPlaceholderConfigurer bean création de ce fichier de configuration - valeur du champ est nulle à nouveau.

Comme solution de contournement, j'utilise cette ligne de code:

System.getProperties().getProperty("property", "defaultValue")

Qui est fonctionne aussi, mais j'aimerais savoir pourquoi un tel comportement ne se produit et peut-être qu'il est possible de réécrire une autre manière mais sans xml?

source d'informationauteur Oleksii Duzhyi