0

I'm new to spring and am working on an app that uses multiple application contexts, configured using annotation.

I have one context where I am creating 3 singleton beans, one of which I want to pass as an argument into the factory method for a prototype bean which will live in a different application context.

This other application context is created as one of the singleton beans within this original context.

The problem I am seeing is that, at the point at which I try to use getBean() to create this other bean that lives in this second context (see the 'someBean()' factory method below), I get an exception from the framework:

Error creating bean with name 'someBean' defined in class org.imaginary.SpringAppDependencyConfiguration: Instantiation of bean failed; nested exception is...Unsatisfied dependency expressed through constructor argument with index 0 of type [org.imaginary.ISomeDependency]: : No qualifying bean of type [org.imaginary.ISomeDependency] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

What have I jacked up here?

The config for the original context looks like so:

@Configuration
public class SpringAppDependencyConfiguration
{
    @Autowired
    private ISomeDependency someDependency;

    @Autowired
    private AnnotationConfigApplicationContext otherSpringContext;

    @Bean(destroyMethod="close")
    public ISomeDependency someDependency()
    {
        return new SomeDependencyImpl( 13 );
    }

    @Bean (destroyMethod="close")
    public AnnotationConfigApplicationContext otherSpringContext()
    {
        return new AnnotationConfigApplicationContext(OtherContextDependencyConfiguration.class);
    }

    @Bean
    @DependsOn( { "otherSpringContext", "someDependency" } )
    public ISomeBean someBean() throws Exception
    {
        if ( !otherSpringContext.getBeansOfType( ISomeOtherBean.class ).containsKey( "SomeOtherBean" ) )
        {
            throw new Exception("SpringAppDependencyConfiguration.someBean(): " +
            "unable to find SomeOtherBean implementation");
        }

        ISomeOtherBean someOtherBean = (ISomeOtherBean) otherSpringContext.getBean( "SomeOtherBean", someDependency );

        return new SomeBeanImpl( someDependency, someOtherBean );
    }

}

The config for the other application context looks like so:

@Configuration
public class OtherContextDependencyConfiguration
{
    @Bean
    @Scope("prototype")
    public ISomeOtherBean someOtherBean(ISomeDependency theDependency) throws Exception
    {
         return new SomeOtherBeanImpl(theDependency);
    }
}
Hoobajoob
  • 2,748
  • 3
  • 28
  • 33
  • Do you truly have multiple contexts or just multiple configuration files, because there is a difference. – bh5k Dec 03 '13 at 17:17
  • Multiple contexts - if you look at the first config class you can see where the second context is instantiated using the second config class - it's a bean within the first context (see the "otherSpringContext" factory method). – Hoobajoob Dec 03 '13 at 17:27

1 Answers1

0

So, I think I learned that one way I can get the result I was looking for is to create this second context in such a way that it treats the initial context as a parent.

To support this required a different conceptual organization of the classes, so I will try to post an update to this question with my current approach once I've got a digestible version of it.

Hoobajoob
  • 2,748
  • 3
  • 28
  • 33