Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
What's the best way to add objects to an existing JavaConfigApplicationContext? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What's the best way to add objects to an existing JavaConfigApplicationContext?

    Sorry if this is obvious- I'm new to JavaConfig:

    If I have a config class something like this:

    @Configuration
    public class Config {
    @Bean
    public Foo foo() {
    return new Foo(...);
    }
    }

    and I create and use my context like this:

    JavaConfigApplicationContext context =
    new JavaConfigApplicationContext(Config.class);

    Foo foo = context.getBean(Foo.class);

    What is the best way to _programatically_ add in a (singleton) object that the Foo constructor depends on? (Something that can't be created/defined in the Config class- it's provided to my code at the time I set up my context).

    I assume I need to do something like this in my Config code:

    @Configuration
    public class Config extends ConfigurationSupport {
    @Bean
    public Foo foo() {
    return new Foo( this.getBean(Bar.class) );
    }
    }

    and then tried doing something like:

    JavaConfigApplicationContext context =
    new JavaConfigApplicationContext(Config.class);

    context.getBeanFactory().registerSingleton("bar", bar);

    Foo foo = context.getBean(Foo.class);

    But this didn't work. Do I have to create a parent context for the javaconfig context? Is there no other way to register an object so that my Configuration class code can access it?

    Thanks,
    -Dave Fogel

  • #2
    Hi Dave,

    The following will work:

    Code:
    JavaConfigApplicationContext ctx = new JavaConfigApplicationContext();
    ctx.addConfigClass(Config.class);
    ctx.registerSingleton("bar", bar);
    ctx.refresh();
    Foo foo = ctx.getBean(Foo.class);
    This follows the general contract of 'refreshable' application contexts in spring. The key is to create your JavaConfigApplicationContext with no arguments. This leaves the context open for further configuration, after which you may manually refresh.

    Comment


    • #3
      Hi Chris-

      Thanks for responding. But actually I don't think that works, at least not as-is. There isn't a "registerSingleton" method on JavaConfigApplicationContext.

      I did try this approach with using:

      ctx.getBeanFactory().registerSingleton("bar", bar);

      but that didn't work- gave me an IllegalStateException. Are you sure your code above would work with the 1.0.0.M4?

      -Dave Fogel

      Comment


      • #4
        Ah, sorry. I was a bit hasty there!

        This will work:

        Code:
        Foo foo = new Foo();
        
        JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(Config.class) {
            @Override
            protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
                beanFactory.registerSingleton("foo", foo);
            }
        };
        
        Foo foo = ctx.getBean(Foo.class);

        Comment


        • #5
          Thanks Chris, I'll try that.

          It does seem like this would be a pretty common thing for users to have to do. Maybe there should be an easier way, or at least this sort of thing could go in the docs?

          thanks again,
          -Dave Fogel

          Comment


          • #6
            Feel free to add an issue to this effect. As far as an easier way, we probably won't change how the internal bean factory gets customized. This is a pretty well-established pattern in the ApplicationContext hierarchy. We could expose a constructor that accepts a BeanFactory, however. This would allow users to register singletons, bean defs, etc against a standalone BeanFactory and then provide that during construction of the context. This would avoid any unnecessary hierarchical contexts and avoid the anonymous subclassing of JCAC.

            Comment

            Working...
            X