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);
}
}