代理及注入之org.springframework.beans.factory.BeanNotOfRequiredTypeException (转载)

错误信息:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘aisleService‘ must be of type [com.gdie.whlocation.service.impl.AisleService], but was actually of type [$Proxy38]

这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,Spring使用JDK的动态代理注入,jdk的动态代理不支持类注入,只支持接口方式注入

看使用的代码:

    1. 使用类注入:
    @Resource(name = "aisleService")
    private AisleService aisleService;  

    2. 使用接口注入:
    @Resource(name = "aisleService")
    private IAisleService aisleService;  

代码1不能使用JDK的动态代理注入,原因是jdk的动态代理不支持类注入,只支持接口方式注入;

代码2可以使用jdk动态代理注入;

如果要使用代码1的方式,必须使用cglib代理;

当然了推荐使用代码2的方式,基于接口编程的方式!

关于spring动态代理的配置:

    1.使用aop配置:
        <aop:config proxy-target-class="false"> </aop:config>   

    2. aspectj配置:
        <aop:aspectj-autoproxy proxy-target-class="true"/>  

    3. 事务annotation配置:
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  

3种配置,只要使用一种即可,设置proxy-target-class为true即使用cglib的方式代理对象。

 附:spring的aop代理判断逻辑:

    //org.springframework.aop.framework.DefaultAopProxyFactory     

    //参数AdvisedSupport 是Spring AOP配置相关类     

    public AopProxy createAopProxy(AdvisedSupport advisedSupport)     

            throws AopConfigException {     

        //在此判断使用JDK动态代理还是CGLIB代理     

        if (advisedSupport.isOptimize() || advisedSupport.isProxyTargetClass()     

                || hasNoUserSuppliedProxyInterfaces(advisedSupport)) {     

            if (!cglibAvailable) {     

                throw new AopConfigException(     

                        "Cannot proxy target class because CGLIB2 is not available. "    

                                + "Add CGLIB to the class path or specify proxy interfaces.");     

            }     

            return CglibProxyFactory.createCglibProxy(advisedSupport);     

        } else {     

            return new JdkDynamicAopProxy(advisedSupport);     

        }     

    }  
时间: 2024-08-27 08:18:36

代理及注入之org.springframework.beans.factory.BeanNotOfRequiredTypeException (转载)的相关文章

Spring AOP基于注解的“零配置”方式---org.springframework.beans.factory.BeanNotOfRequiredTypeException

具体异常信息如下: 1 Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'hello' must be of type [com.hyq.chapter08_04_3.HelloImpl], but was actually of type [com.sun.proxy.$Proxy13] 2 at org.springfram

org.springframework.beans.factory.BeanDefinitionStoreException异常

1.下面是我遇到的异常信息: 2017-03-25 18:01:11,322 [localhost-startStop-1] [org.springframework.web.context.ContextLoader]-[ERROR] Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'da

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;testService&#39; is defined

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defined(1)这个错误的原因是编译器找不到bean 的注入,还可能是你的注入名字写错了. (2)spring的配置文件 书写错误,没有加入自动扫描功能 这句<context:component-scan base-package="包名"></context:compone

org.springframework.beans.factory.parsing.BeanDefinitionParsingException

今天在练习spring aop时.调试程序出现下面错误 org.springframework.beans.factory.parsing.BeanDefinitionParsingException 百度了非常多方法都没有解决问题,在使用Spring时我是直接使用MyEcplise将Spring的jar包导入,Spring的版本号用的是2.5.6的,然后手动导入了aspectjrt.jar和aspectjweaver.jar两个jar包.网上有非常多人说是jar包冲突,我将自己导入的jar包删

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;springSessionRepositoryFilter&#39; is defined

spring-session 集成redis,web.xml配置filter时候出现  No bean named 'springSessionRepositoryFilter' is defined 从spring给出的异常可以看出是没有取到这个bean,然后纠结了半天,各种版本切换找问题依旧未解决,然后Google找了半天, 也是没有找到问题解决方式,stackoverflow上也有许多人提出该问题,但是看了下面的解决方式,然并卵,一个都没用... 只能试着自己解决了. 既然没找到bean,

添加事务后 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type available

今天遇到了一个奇怪的问题   在没添加事务之前  所有的代码都是好的 , 当我添加了事务之后, 代码报错 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type available ApplicationContext: <!-- 配置事务 --> <bean id="transactionManager" class="org.s

ssm整合时出现 org.springframework.beans.factory.BeanCreationException :Error creating bean with name ‘XXX’ 异常的原因及解决方法

ssm整合时出现 org.springframework.beans.factory.BeanCreationException :Error creating bean with name 'XXX' 异常的原因及解决方法(只是可能出现下列几种,不包含全部) 此异常为:注入 bean 失败异常,也就是找不到注入的bean. 可能有以下几种原因: 1.bean未注解或者注解错误 2.项目整合的时候jar包冲突 3.'XXX'的配置有错误 解决:1,3仔细检查就是,网上大部分的人应该是2这种错误,

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dx.service.ItemService] found for dependency

在整合ssm框架,测试service层的时候报错 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dx.service.ItemService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dep

org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type &#39;xxx &#39;available

搭了一个简单的SSM项目,启动报错: org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.xxx.ssmdemo.service.IUserService' available: expected at least 1 bean which qualifies as autowire candidate.Dependency annotations: {@o