Results 1 to 3 of 3

Thread: post-construction configuration behavior

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Location
    Billings, Montana
    Posts
    47

    Default post-construction configuration behavior

    Post construction configuration does not retain original configurations. I am not sure if this is the expected behavior. I am using M4.

    Here is my test:

    JavaConfigApplicationContext initialContext = new JavaConfigApplicationContext(ThreadConfig.class);

    JavaConfigApplicationContext finalContext = new JavaConfigApplicationContext();
    finalContext.setParent(initialContext);
    finalContext.addConfigClass(AppConfig.class);
    finalContext.addConfigClass(DataConfig.class);
    finalContext.refresh();

    But, when I debug, I do not see the ThreadConfig in the finalContext configClasses. So when I refresh finalContext, my initial context configClasses are not retained? Also, I tried setAllowBeanDefinitionOverriding to true, but that didn't help either.

    On a similar note, probably section, 3.1.1.3. "Post-construction configuration" needs to update the code sample to use the addConfigClass API, right?.

    I don't think setConfigClasses is a valid API in JavaConfigApplicationContext anymore since M4.

    Appreciate your help.

    Best regards
    Arul

  2. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    Hi Arul,

    Working against the latest M5 snapshot, the following works fine for me
    Code:
    package org.springframework.config.java.context;
    
    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.junit.Assert.assertThat;
    
    import org.junit.Test;
    import org.springframework.config.java.annotation.Bean;
    import org.springframework.config.java.annotation.Configuration;
    
    public class TempTests {
        @Test
        public void test() {
            JavaConfigApplicationContext initialContext = new JavaConfigApplicationContext(ThreadConfig.class); 
            
            JavaConfigApplicationContext finalContext = new JavaConfigApplicationContext();
            finalContext.setParent(initialContext);
            finalContext.addConfigClass(AppConfig.class);
            finalContext.addConfigClass(DataConfig.class);
            finalContext.refresh();
            
            finalContext.getBean(ThreadConfig.class);
            finalContext.getBean(AppConfig.class);
            finalContext.getBean(DataConfig.class);
            
            assertThat(finalContext.getBean(String.class), equalTo("foostring"));
        }
        
    }
    
    @Configuration class ThreadConfig {
        public @Bean String foo() { return "foostring"; }
    }
    @Configuration class AppConfig { }
    @Configuration class DataConfig { }
    Thanks also for the correction on the documentation. It has been updated to reflect using addConfigClass() and addBasePackage() vs the older-style methods that have now been removed.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Aug 2008
    Location
    Billings, Montana
    Posts
    47

    Default

    Thanks Chris for the update.

    I will try with M5.

    -Arul

Posting Permissions

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