Question

Spring appears to scan for and pick up my bean, like so:

16:38:18.328 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'ABCProvider'
16:38:18.328 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'ABCProvider'
16:38:18.334 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'getABCSupplier'
16:38:18.334 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'getABCSupplier'
16:38:18.336 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'getABCSupplier' to allow for resolving potential circular references
16:38:18.340 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
16:38:18.341 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'getABCSupplier'
16:38:18.341 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'strategyDAO'
16:38:18.346 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'subStrategyDAO'
16:38:18.346 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'subStrategyDAO'
16:38:18.347 [MainThread] DEBUG  o.s.b.f.annotation.InjectionMetadata - Found injected element on class [com.foo.bar.strategy.dataaccess.hibernate.subStrategyHibernateDAO]: AutowiredMethodElement for public void com.foo.common.dataaccess.hibernate.GenericHibernateDAO.setSessionFactory(org.hibernate.SessionFactory)
16:38:18.347 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'subStrategyDAO' to allow for resolving potential circular references
16:38:18.353 [MainThread] DEBUG  o.s.b.f.annotation.InjectionMetadata - Processing injected method of bean 'subStrategyDAO': AutowiredMethodElement for public void com.foo.common.dataaccess.hibernate.GenericHibernateDAO.setSessionFactory(org.hibernate.SessionFactory)
16:38:18.353 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'sessionFactory'
16:38:18.353 [MainThread] DEBUG  o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'subStrategyDAO' to bean named 'sessionFactory'
16:38:18.353 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
16:38:18.354 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'subStrategyDAO'
16:38:18.354 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'ABCProvider' via constructor to bean named 'getABCSupplier'
16:38:18.354 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'ABCProvider' via constructor to bean named 'strategyDAO'
16:38:18.354 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Autowiring by type from bean name 'ABCProvider' via constructor to bean named 'subStrategyDAO'
16:38:18.505 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'ABCProvider' to allow for resolving potential circular references
16:38:18.509 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
16:38:18.510 [MainThread] DEBUG  o.s.t.a.AnnotationTransactionAttributeSource - Adding transactional method 'getABCEntities' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
16:38:18.510 [MainThread] DEBUG  o.s.a.f.a.InfrastructureAdvisorAutoProxyCreator - Creating implicit proxy for bean 'ABCProvider' with 0 common interceptors and 1 specific interceptors
16:38:18.510 [MainThread] DEBUG  o.s.aop.framework.JdkDynamicAopProxy - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.foo.bar.abc.ABCProvider@35fe3a7e]
16:38:18.514 [MainThread] DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'ABCProvider'

And then later on fails with this error:

16:38:20.512 [MainThread] ERROR  c.b.functional.main.java.MainThread - UnsatisfiedDependencyException: Error creating bean with name 'abcDataProvider' defined in class com.foo.bar.spring.ServerRPCConfig: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.foo.bar.abc.ABCProvider]: : No matching bean of type [com.foo.bar.abc.ABCProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.bar.abc.ABCProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Throwable=org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'abcDataProvider' defined in class com.foo.bar.spring.ServerRPCConfig: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.foo.bar.abc.ABCProvider]: : No matching bean of type [com.foo.bar.abc.ABCProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.bar.abc.ABCProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:461)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at com.foo.foundations.springconfig.SpringApplication.start(SpringApplication.java:57)
    at com.foo.foundations.springconfig.SpringBazMain.init(SpringBazMain.java:53)
    at com.foo.bar.config.server.ConfigServerMain.init(ConfigServerMain.java:61)
    at com.baz.functional.main.java.MainThread.init(MainThread.java:103)
    at com.baz.support.event.java.EventThread.run(EventThread.java:328)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.bar.abc.ABCProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:948)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:817)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
    ... 18 more

Injection point which fails:

@Inject
@Bean
@NotNull
public SimpleMessageListenerContainer abcDataProvider(
        @Named(RPCConfig.QUEUE_BAR_ABC_RPC) @NotNull ActiveMQQueue aQueue,
        @NotNull ABCProvider aProvider) {
    return createMessageListenerContainer(aQueue,
            ABCProvider.class,
            aProvider);
}

ABCProvider:

@Service
public class ABCProvider implements ABCDataProvider<Config> {
    ABCDataProviderImpl<ABC, Config> theProvider;

    @Inject
    public ABCProvider(
            @NotNull Supplier<ABC> aABC,
            @NotNull StrategyDAO aStrategyDAO,
            @NotNull SubStrategyDAO aSubStrategyDAO) {
        theProvider = new ABCDataProviderImpl<>(
                aABC,
                aStrategyDAO,
                aSubStrategyDAO,
                createTransform(),
                ABCSelectorBuilder.supplier(aABC));
    }

    /* ... */

    @Bean
    public static Supplier<ABC> getABCSupplier() {
        return Suppliers.memoize(new ABCSupplier());
    }
}
Was it helpful?

Solution

It looks like, for whatever reason, your ABCProvider bean was proxied.

16:38:18.510 [MainThread] DEBUG  o.s.aop.framework.JdkDynamicAopProxy - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.foo.bar.abc.ABCProvider@35fe3a7e]

Spring used JDK proxies which don't extend your type's class. They only implement your type' interfaces. As such, when Spring tries to resolve a bean of type ABCProvider to inject into your other @Bean method, it doesn't find one.

One possible solution is to use CGLIB proxies. You can make that change in your configuration.

Another solution is to declare the interface type of your ABCProvider as the injection target type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top