Results 1 to 6 of 6

Thread: Placeholders in Imports

  1. #1
    Join Date
    Feb 2012
    Posts
    2

    Default 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
    Attached Files Attached Files
    Last edited by tcoup; Feb 7th, 2012 at 03:22 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    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
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  3. #3
    Join Date
    Feb 2012
    Posts
    2

    Default

    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

  4. #4
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Yes, as the blog says, we can use a ApplicationContextInitializer to add a PropertySource to Enviroment.
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

  5. #5
    Join Date
    Feb 2012
    Posts
    1

    Default

    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.

  6. #6
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default 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
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

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
  •