Spring实例化bean但不能使用它

时间:2014-01-27 16:09:21

标签: java spring annotations autowired

Spring似乎扫描并拿起我的bean,就像这样:

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'

然后稍后因此错误而失败:

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

注射点失败:

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

1 个答案:

答案 0 :(得分:2)

看起来,无论出于何种原因,您的ABCProvider bean都被代理了。

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使用的JDK代理不扩展你的类的类。它们只实现您的类型'接口。因此,当Spring尝试解析类型ABCProvider的bean以注入您的其他@Bean方法时,它找不到一个。

一种可能的解决方案是使用CGLIB代理。您可以在配置中进行更改。

另一种解决方案是将ABCProvider的接口类型声明为注入目标类型。