문제

I'm trying to wrap my head around how Spring resolves property placeholders when not explicitly declaring a PropertySourcesPlaceholderConfigurer bean. Looking through the source of an existing project that configures spring through java via annotations. . .

in the spring context xml:

<context:component-scan base-package="com.myproject.config" />

and in a java file that bootstraps the rest of the app & configurations

package com.myproject.config;

@Configuration
@ComponentScan(basePackages = {"com.myproject.app"})
@PropertySource("config/${app.env}.properties")
public class RootConfig {

}

It's all very slick, but I cannot for the life of me figure out what tells Spring to evaluate the ${...} property placeholder syntax against environment variables. I've been unable to find the answer in the spring documentation, though I understand spring relies on the PropertySourcesPlaceholderConfigurer class to do this. Nothing clues me in on when/how this class is invoked implicitly. Is it through the @Configuration annotation, or is it in another part of the spring bootstrapping process?

I know this isn't the most pertinent thing to understand, but I dislike writing anything off as "Spring Magic". Any insight into this would be amazing!

올바른 솔루션이 없습니다

다른 팁

I would try to do that:

@PropertySource("config/#{ systemProperties['app.env'] }.properties")

(source: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-beandef-xml-based) Let me know if it is evaluated.

Per Spring @Configuration documentation, your RootConfig class has to be "bootstrapped" from somewhere, below are some examples.

via AnnotationConfigApplicationContext

AnnotationConfigApplicationContext context = 
       new AnnotationConfigApplicationContext();
ccontext.register(RootConfig.class);

via Spring xml

<beans>
  <context:annotation-config/>
  <bean class="...RootConfig"/>
</beans>

Have you checked the source that's bootstrapping RootConfig to see if a PropertySourcesPlaceholderConfigurer has been declared? For example:

<context:property-placeholder/>
    @Configuration
@PropertySource("classpath:property.property")
public class ConfigClass {

    @Autowired Environment env;

    @Bean
    public Entity getEntity(){
        Entity entity = new Entity();
        entity.setUsername(env.getProperty("jdbc.username"));
        entity.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        entity.setPassword(env.getProperty("jdbc.password"));
        entity.setUrl(env.getProperty("jdbc.url"));
        return entity;
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top