Announcement Announcement Module
Collapse
No announcement yet.
Placeholders in Imports Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Placeholders in Imports

    Evening all,

    I can't get this to work at all and hoping someone can just point out what i'm doing wrong from the simple attached project.

    I'm trying to select a subcontext to import into the main context with the subcontext name coming from a properties file.

    I've solved the actual problem using the new profile selection method, but really want to get some closure on this!

    Thanks for any input,

    Tom
    Last edited by tcoup; Feb 7th, 2012, 03:22 PM.

  • #2
    The contextroperty-placeholder creates a PropertySourcesPlaceholderConfigurer that is a BeanFactoryPostProcessor that do his work after reading the context xml file and their properties are not available when resolving imports placeholders.

    What properties are avaliable in imports placeholders depends of Enviroment implementation returned by AbstractApplicationContext.createEnviroment() (StandarEnviroment, StandarServeltEnviroment...)

    See if the Enviroment that is using your context is suitable for your needs, if not overwrite createEnviroment and return your own CustomEnviroment.

    Cheers

    Comment


    • #3
      Thanks for the reply.

      I was under the impression (http://blog.springsource.org/2011/02...ty-management/) that the restriction had been lifted in 3.1 as part of the work around unifying the placeholder system.

      The section on imports and placeholders to me that suggests this should work out of the box and is not something we should have to manipulate the environment implementation for.

      Thanks

      Comment


      • #4
        Yes, as the blog says, we can use a ApplicationContextInitializer to add a PropertySource to Enviroment.

        Comment


        • #5
          I was under the same impression as Tom from that blog post. Perhaps this is wrong, but I read it as the new PropertySourcesPlaceholderConfigurer used by default now in 3.1 via <contextroperty-placeholder location="..."/> would add any location entries into a PropertySource that the Environment was aware of. I've been trying to get exactly the same thing working as Tom posted, but have been unsuccessful.

          Comment


          • #6
            Why? that's solved

            Why? The problem is already solved. In web enviroment, use an ApplicationContextInitializer to add a PropertySource to Environment

            Code:
            <context-param>
                <param-name>contextInitializerClasses</param-name>
                <param-value>com.bank.MyInitializer</param-value>
            </context-param>
            
            public class MyInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
                public void initialize(ConfigurableWebApplicationContext ctx) {
                    PropertySource ps = new MyPropertySource();
                    ctx.getEnvironment().getPropertySources().addFirst(ps);
                    // perform any other initialization of the context ...
                }
            }
            On other scenarios simply use the same code after context creation:

            Code:
             ApplicationContext ctx = new ClassPathXmlApplicationContext(...);
             Properties prop = loadProperties(); 
             PropertySource ps = PropertiesPropertySource("init_properties", prop);
             ctx.getEnvironment().getPropertySources().addFirst(ps);
            ...
            Cheers

            Comment

            Working...
            X