本文介绍了春豆实例化,但不能使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

春季出现扫描并拿起我的绿豆,就像这样:

  16:38:18.328 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory  - 创建单bean的共享实例ABCProvider
16:38:18.328 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 创建bean的实例ABCProvider
16:38:18.334 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 创建单bean的共享实例getABCSupplier
16:38:18.334 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 创建bean的实例getABCSupplier
16:38:18.336 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 急切缓存豆getABCSupplier,以便解决潜在的循环引用
16:38:18.340 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 选举单bean的缓存实例org.springframework.transaction.config.internalTransactionAdvisor
16:38:18.341 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 完成创建bean的实例getABCSupplier
16:38:18.341 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 选举单bean的缓存实例strategyDAO
16:38:18.346 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 创建豆单subStrategyDAO'的共享实例
16:38:18.346 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 创建bean的实例subStrategyDAO
16:38:18.347 [MainThread] DEBUG o.s.b.f.annotation.InjectionMetadata - 阶级[com.foo.bar.strategy.dataaccess.hibernate.subStrategyHibernateDAO]发现注入的因素:公共无效AutowiredMethodElement com.foo.common.dataaccess.hibernate.GenericHibernateDAO.setSessionFactory(org.hibernate.SessionFactory)
16:38:18.347 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 急切缓存豆subStrategyDAO,以便解决潜在的循环引用
16:38:18.353 [MainThread] DEBUG o.s.b.f.annotation.InjectionMetadata - 豆subStrategyDAO处理方法注入:公共无效AutowiredMethodElement com.foo.common.dataaccess.hibernate.GenericHibernateDAO.setSessionFactory(org.hibernate.SessionFactory)
16:38:18.353 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 选举单bean的缓存实例SessionFactory的
16:38:18.353 [MainThread] DEBUG o.s.b.f.a.AutowiredAnnotationBeanPostProcessor ​​ - 自动装配由bean的名字'subStrategyDAO来命名的bean类型'SessionFactory的
16:38:18.353 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 选举单bean的缓存实例org.springframework.transaction.config.internalTransactionAdvisor
16:38:18.354 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 完成创建bean的实例subStrategyDAO
16:38:18.354 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 通过自动装配通过构造名为豆从bean的名字'ABCProvider类型getABCSupplier
16:38:18.354 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 通过自动装配通过构造一个名为strategyDAO'豆从bean的名字'ABCProvider类型
16:38:18.354 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 通过自动装配通过构造函数豆从bean的名字'ABCProvider类型命名为subStrategyDAO
16:38:18.505 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 急切缓存豆ABCProvider,以便解决潜在的循环引用
16:38:18.509 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 选举单bean的缓存实例org.springframework.transaction.config.internalTransactionAdvisor
16:38:18.510 [MainThread] DEBUG o.s.t.a.AnnotationTransactionAttributeSource - 添加事务法getABCEntities'与属性:PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
16:38:18.510 [MainThread] DEBUG o.s.a.f.a.InfrastructureAdvisorAutoProxyCreator - 用于创建豆隐含代理ABCProvider0共同拦截器和1拦截特定
16:38:18.510 [MainThread] DEBUG o.s.aop.framework.JdkDynamicAopProxy - 创建JDK动态代理:目标源是SingletonTargetSource为目标对象[com.foo.bar.abc.ABCProvider@35fe3a7e]
16:38:18.514 [MainThread] DEBUG o.s.b.f.s.DefaultListableBeanFactory - 完成创建bean的实例ABCProvider

再后来就失败,此错误:

  16:38:20.512 [MainThread]错误cbfunctional.main.java.MainThread  -  UnsatisfiedDependencyException:类com.foo.bar.spring定义错误创建一个名为豆'abcDataProvider .ServerRPCConfig:[com.foo.bar.abc.ABCProvider]通过构造函数参数类型的索引1 pssed不满意依赖前$ p $:[com.foo.bar.abc.ABCProvider]发现依赖型不匹配豆:预计至少有1 Bean上有资格作为自动装配候选人这种依赖性。依赖注解:{};嵌套的例外是org.springframework.beans.factory.NoSuchBeanDefinitionException:无类型[com.foo.bar.abc.ABCProvider]匹配豆找到了依赖性:预计至少有1 Bean上有资格作为自动装配候选人这种依赖性。依赖注解:{}
可抛出= org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建一个名为豆'abcDataProvider类com.foo.bar.spring.ServerRPCConfig定义:不满意依赖前$ P $通过构造函数pssed型指数1 [COM .foo.bar.abc.ABCProvider]:类型发现依赖性[com.foo.bar.abc.ABCProvider]中没有匹配的bean:预计至少有1 Bean上有资格作为自动装配候选人这种依赖性。依赖注解:{};嵌套的例外是org.springframework.beans.factory.NoSuchBeanDefinitionException:无类型[com.foo.bar.abc.ABCProvider]匹配豆找到了依赖性:预计至少有1 Bean上有资格作为自动装配候选人这种依赖性。依赖注解:{}
    在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730)
    在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:461)
    在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
    在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    在org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    在org.springframework.beans.factory.support.DefaultListableBeanFactory.$p$pInstantiateSingletons(DefaultListableBeanFactory.java:605)
    在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    在com.foo.foundations.springconfig.SpringApplication.start(SpringApplication.java:57)
    在com.foo.foundations.springconfig.SpringBazMain.init(SpringBazMain.java:53)
    在com.foo.bar.config.server.ConfigServerMain.init(ConfigServerMain.java:61)
    在com.baz.functional.main.java.MainThread.init(MainThread.java:103)
    在com.baz.support.event.java.EventThread.run(EventThread.java:328)
    在java.lang.Thread.run(Thread.java:724)
org.springframework.beans.factory.NoSuchBeanDefinitionException:所致[com.foo.bar.abc.ABCProvider]发现依赖型不匹配的bean:预计至少有1 Bean上有资格作为自动装配候选人这种依赖性。依赖注解:{}
    在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:948)
    在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:817)
    在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731)
    在org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
    在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
    ... 18个

注射点,这将失败:

  @Inject
@豆
@NotNull
公共SimpleMessageListenerContainer一样abcDataProvider(
        @Named(RPCConfig.QUEUE_BAR_ABC_RPC)@NotNull ActiveMQQueue aQueue,
        @NotNull ABCProvider aProvider){
    返回createMessageListenerContainer(aQueue,
            ABCProvider.class,
            aProvider);
}

ABCProvider:

  @Service
公共类ABCProvider实现ABCDataProvider<配置> {
    ABCDataProviderImpl< ABC,配置> theProvider;    @注入
    公共ABCProvider(
            @NotNull供应商< ABC> AABC,
            @NotNull StrategyDAO aStrategyDAO,
            @NotNull SubStrategyDAO aSubStrategyDAO){
        theProvider =新ABCDataProviderImpl<>(
                AABC,
                aStrategyDAO,
                aSubStrategyDAO,
                createTransform(),
                ABCSelectorBuilder.supplier(AABC));
    }    / * ... * /    @豆
    公共静态供应商< ABC> getABCSupplier(){
        返回Suppliers.memoize(新ABCSupplier());
    }
}


解决方案

看起来,无论出于何种原因,你的 ABCProvider 豆被代理的。

  16:38:18.510 [MainThread] DEBUG osaop.framework.JdkDynamicAopProxy  - 创建JDK动态代理:目标源是SingletonTargetSource为目标对象[com.foo.bar.abc.ABCProvider @ 35fe3a7e]

春季使用JDK代理不延长你的类型的类。他们只实现你的类型的接口。因此,当春尝试解析类型的豆 ABCProvider 来注入到你的其他 @Bean 方法,它不找不到之一。

一种可能的解决方案是使用CGLIB代理。您可以在您的配置这种变化。

另一个解决方案是申报的接口类型的 ABCProvider 作为注入目标的类型。

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());
    }
}
解决方案

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.

这篇关于春豆实例化,但不能使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 01:30