开发者

How to create annotation-configured beans with an existing instance of @Configuration?

Assume we have a simple @Configuration:

@Configuration
public class FooBarConfiguration {

    @Bean
    public Foo createFoo() {
        return new FooImpl();
    }

    @Bean
    @Autowired
    public Bar createBar(Foo foo) {
        return new BarImpl(foo);
    }
}

This class can be used with AnnotationConfigApplicationContext to produce Foo and Bar instances:

final ApplicationContext applicationContext =
    new AnnotationConfigApplicationContext(FooBarConfiguration.class);

final Foo foo = applicationContext.getBean(Foo.class);
final Bar bar = applicationContext.getBean(Bar.class);
assertSame(foo, bar.getFoo());

In the example above Spring will create a new instance of FooBarConfiguration and use it to produce Foos and Bars.

Now assume we already have an instance of FooBarConfiguration and we want to create Foos and Bars through Spring with this very instance. Is there a way to accomplish this?

How to create annotation-configured beans with an existing instance of configuration object?


ps. With Google Guice the solution is trivial:

public class FooBarConfiguration implements Module {

    @Provides
    @Singleton
    public Foo createFoo() {
        return new FooImpl();
    }

    @Override
    public void configure(Binder binder) {开发者_JAVA技巧
    }

    @Provides
    @Inject
    @Singleton
    public Bar createBar(Foo foo) {
        return new BarImpl(foo);
    }
}

final FooBarConfiguration fooBarConfiguration = ...; // Our instance

final Injector injector = Guice.createInjector(fooBarConfiguration);
final Foo foo = injector.getInstance(Foo.class);
final Bar bar = injector.getInstance(Bar.class);
assertSame(foo, bar.getFoo());


It's impossible, because some features of Spring's @Configuration require class enhancement, that can't be done on the existing instance.

For example, you can rewrite your configuration as follows:

@Configuration 
public class FooBarConfiguration { 

    @Bean 
    public Foo createFoo() { 
        return new FooImpl(); 
    } 

    @Bean 
    public Bar createBar() { 
        return new BarImpl(createFoo()); 
    } 
} 

and you'll still get a fully initialized instance of Foo, because call to createFoo() will be intercepted.


Here's my own variant:

    @Test
    public void createFromConfigurationInstance() {

        final FooBarConfiguration fooBarConfiguration = new FooBarConfiguration();

        final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        final AnnotatedBeanDefinitionReader annotatedBeanDefinitionReader = new AnnotatedBeanDefinitionReader(
                beanFactory);

        annotatedBeanDefinitionReader.register(FooBarConfiguration.class);

        beanFactory.registerSingleton("fooBarConfiguration",
                fooBarConfiguration);

        final ConfigurableApplicationContext applicationContext = new GenericApplicationContext(
                beanFactory);

        applicationContext.refresh();

        final Foo foo = applicationContext.getBean(Foo.class);
        final Bar bar = applicationContext.getBean(Bar.class);
        assertSame(foo, bar.getFoo());

    }

However, I'm not sure if it is a "good" or "official" way.


In Spring you basically have to either:

  1. Force your FooBarConfiguration to be a Singleton (not relying on the IoC container to enforce this - e.g. implement using enum, or another Singleton pattern)

  2. Wrap your FooBarConfiguration in a FooBarConfigurationFactory and use something like MethodInvokingFactoryBean to call it.

  3. Expose a static factory method, directly on FooBarConfiguration, and use the same technique as #2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜