Springboot autowired beannotofrequiredtypeexception

Source: Internet
Author: User

Phenomenon
Org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean named ' Xxxximpl ' is expected to be of type ' Com.xxx.xxxImpl ' but is actually of type ' com.sun.proxy. $Proxy 62 '

Directly autowired an implementation class, not an interface

@Autowiredprivate Xxxserviceimpl Xxxservice;
Solution Solutions

1. Autowired interface

2. Using Enableaspectjautoproxy

true Publicclass  application {    publicstaticvoid Main (string[] args) {        new springapplication (application.  Class);        App.run (args);}    }

Setting Proxy-target-class to True is the proxy object using Cglib, which by default is the JDK mode proxy.

The JDK's dynamic proxy does not support class injection, only interface-mode injection is supported.

Dynamic Proxy type judgment
//org.springframework.aop.framework.DefaultAopProxyFactory//parameter Advisedsupport is a spring AOP configuration related class   Publicaopproxy createaopproxy (advisedsupport advisedsupport)throwsaopconfigexception {//determine whether to use the JDK dynamic agent or the Cglib agent      if(Advisedsupport.isoptimize () | |Advisedsupport.isproxytargetclass ()||hasnousersuppliedproxyinterfaces (Advisedsupport)) {               if(!cglibavailable) {                   Throw NewAopconfigexception ("Cannot proxy target class because CGLIB2 is not available." + "Add CGLIB to the class path or specify proxy interfaces."); }               returnCglibproxyfactory.createcglibproxy (Advisedsupport); } Else {               return NewJdkdynamicaopproxy (Advisedsupport); }       }  

Springboot autowired beannotofrequiredtypeexception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.