Announcement Announcement Module
Collapse
No announcement yet.
why doesn't property-placeholder register PropertySource with Environment? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why doesn't property-placeholder register PropertySource with Environment?

    trying to wrap my brain around Spring 3.1's Environment + Profiles + PropertySource concepts and don't quite understand why declaring a contextroperty-placeholder does NOT automatically register underlying PropertySource with ctx's Environment?

    Doing this in xml config:
    Code:
    <context:property-placeholder location="classpath:myapp.properties"/>
    allows, among other things, to resolve @Value-annotated bean props, for example in a spring bean class:
    Code:
    @Value("${myprop1}") //assume declared in myapp.properties
    private String prop1Value;
    
    @Autowired
    private ConfigurableApplicationContext ctx;
    ...yet, inside the same bean, the following will not return true
    Code:
    ctx.getEnvironment().containsProperty("myprop1"); //returns false
    which seems inconsistent v. @Value eval.

    ..unless I write another bean that, at container startup will do something like:
    Code:
            PropertiesPropertySource ps = new PropertiesPropertySource("myapp_props", myappProps);
            ctx.getEnvironment().getPropertySources().addFirst(ps);

    Why wouldn't the last step be done automatically by the framework as result of contextroperty-placeholder declaration?

    just trying to understand the rationale...

  • #2
    PropertyPlaceHolderConfigurer is only used to REPLACE place holders in the configuration is is not to be used to load and expose properties files (that is a different kind of task).

    You can do it the other way around, create a property source and register that with the PPHC.

    Comment

    Working...
    X