由org.springframework.beans.factory.BeanNotOfRequiredTypeException的报错到Spring 注入的延伸

本文深入探讨了Spring框架中Bean的注入机制,包括@Autowired、@Resource和@Qualifier的区别及使用场景,特别关注了接口和实现类的注入问题,以及如何解决多实现类的注入冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息:

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloWorldImpl1' is expected to be of type 'time.HelloWorldImpl1' but was actually of type 'com.sun.proxy.$Proxy25'

 这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码:

  1. 1. 使用类注入:  
  2. @Resource(name = "aisleService")  
  3. private AisleService aisleService;  
  4.   
  5. 2. 使用接口注入:  
  6. @Resource(name = "aisleService")  
  7. private IAisleService aisleService;  

 

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

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

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

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

 

关于spring动态代理的配置:

Xml代码 

  1. 1.使用aop配置:   
  2.     <aop:config proxy-target-class="false"> </aop:config>   
  3.   
  4. 2. aspectj配置:   
  5.     <aop:aspectj-autoproxy proxy-target-class="true"/>  
  6.       
  7. 3. 事务annotation配置:   
  8.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  

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

问题:spring注入接口还是实现类,同一接口有多个实现类,如何注入

1、接口 HelloWorld

public interface HelloWorld {
    void printHelloWorld();

    void doPrint();
}

2.1、实现类:HelloWorldImpl1,实现了HelloWorld接口

@Service("testHelloWorldImpl1")
public class HelloWorldImpl1 implements HelloWorld
{
    public void printHelloWorld()
    {
        System.out.println("Enter HelloWorldImpl1.printHelloWorld()");
    }

    public void doPrint()
    {
        System.out.println("Enter HelloWorldImpl1.doPrint()");
        return ;
    }
}

3、业务类:TimeTest

public class TimeTest {
    @Autowired
    HelloWorld helloWorld
;            
}

假如有一个接口 HelloWorld, HelloWorldImpl1类实现了接口 HelloWorld, 且该接口只有 HelloWorldImpl1这一个实现类,那么在引用实现类的时候,我们使用的是实现类的接口(像上面程序展示的那样)。Spring会按 byType的方式寻找接口的实现类,将其注入。

假如有另一个实现类 HelloWorldImpl2也实现了接口 HelloWorld, 这时候再按上面的方式去引用, 在同时存在两个实现类的情况下,会报错。

2.2、实现类:HelloWorldImpl2,实现了HelloWorld接口

@Service("testHelloWorldImpl2")
public class HelloWorldImpl2 implements HelloWorld
{
    public void printHelloWorld()
    {
        System.out.println("Enter HelloWorldImpl2.printHelloWorld()");
    }

    public void doPrint()
    {
        System.out.println("Enter HelloWorldImpl2.doPrint()");
        return ;
    }
}

因为@Autowired 的注入方式是 byType 注入, 当要注入的类型在容器中存在多个时,Spring是不知道要引入哪个实现类的,所以会报错。

可以通过 byName 注入的方式。可以使用 @Resource 或 @Qualifier 注解

@Resource 默认是按照 byName 的方式注入的, 如果通过 byName 的方式匹配不到,再按 byType 的方式去匹配。所以上面的引用可以替换为:

public class TimeTest {
    @Resource(name="testHelloWorldImpl1")
    HelloWorld helloWorldImpl1;    @Resource(name="testHelloWorldImpl2")    
    HelloWorld helloWorldImpl2;
    ...... 
}

注意:在使用@Resource时,如果我们已经使用@Service指定了bean的名字(2.1中的@Service("testHelloWorldImpl1")、2.2中的@Service("testHelloWorldImpl2")),此时@Resource中bean的名字要与之对应;

未指定bean的名字,因为使用@Service,容器为我们创建bean时默认类名首字母小写,所以@Resource的name应为helloWorldImpl1、helloWorldImpl2不是HelloWorldImpl1、HelloWorldImpl2,即类名的小写

public class TimeTest {
@Resource(name="helloWorldImpl1")
HelloWorld helloWorldImpl1;

@Resource(name="helloWorldImpl2")
  HelloWorld helloWorldImpl2;
......
}

@Qualifier 注解也是 byName的方式,但是与@Resource 有区别,@Qualifier 使用的是 bean的名字。

public class TimeTest {

    @Autowired
    @Qualifier("testHelloWorldImpl1")
    HelloWorld helloWorldImpl1;

    @Autowired
    @Qualifier("testHelloWorldImpl2")   //@Qualifier是根据定义的bean的名字注入
    HelloWorld helloWorldImpl2;
    ......
}

 

总结:

1、@Autowired 是通过 byType 的方式去注入的, 使用该注解,要求接口只能有一个实现类。@Autowired( import org.springframework.beans.factory.annotation.Autowired;)是Spring的注解

2、@Resource 可以通过 byName 和 byType的方式注入, 默认先按 byName的方式进行匹配,如果匹配不到,再按 byType的方式进行匹配。@Resource(import javax.annotation.Resource;)是J2EE的注解

3、@Qualifier 注解可以按名称注入, 但是注意是 类名的首字母小写

Spring属于第三方的,J2EE是Java自己的东西。使用@Resource可以减少代码和Spring之间的耦合。

————————————————
原文链接:https://blog.youkuaiyun.com/z2516305651/article/details/98334939
原文链接:https://blog.youkuaiyun.com/q503385724/article/details/87349369
原文链接:https://blog.youkuaiyun.com/verseboys/article/details/79898906

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值