Results 1 to 2 of 2

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

  1. #1

    Default 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 context:property-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 context:property-placeholder declaration?

    just trying to understand the rationale...

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •