Spring's Applicationcontext.getbean (type) cannot get the bean, error

Source: Internet
Author: User

Please see https://q.cnblogs.com/q/108101/for specific questions.

Studied for two days: the exclusion from the above: the cause of the problem is that there is a cut-in method in the bean to be acquired.

It is possible that the class will use reflection to generate a class.

How to test it?

Think of these two annotations: @Autowired and @Resource.

They will find the corresponding bean in the container by type and name.

Then use this annotation in the controller to inject Zaservice;

The error:

  

caused By:org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean named ' Zaserivce ' is expected to BES of Type' Com.mytest.context.impl.ZaSerivce ' but was actually of type ' com.sun.proxy. $ProxyAt Org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType ( defaultlistablebeanfactory.java:1510) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType ( defaultlistablebeanfactory.java:1517) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound ( defaultlistablebeanfactory.java:1489) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( DEFAULTLISTABLEBEANFACTORY.JAVA:1104) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( defaultlistablebeanfactory.java:1066) at Org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject (autowiredannotationbeanpostprocessor.java:585) ... More

This explains the two questions: 1. Why BYTPE cannot get a bean, because its type is not com.mytest.context.impl.ZaSerivce at all but Com.sun.proxy. $Proxy 15

2. ByName Although it can be obtained but will find that its type is not zaservice anymore.

ApplicationContext context =  Applicationcontextutils.context;        Zaservice z = (zaservice) context.getbean ("Zaservice"); Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot be Cast to Com.mytest.context.impl.ZaService

  

Workaround: 1.    The class of the parameter-passing interface classes when the bean is obtained through bytype. Insufficient: There can be only one implementation class.

2. ByName can normally pass the first letter of the parameter bean lowercase. But can only be converted to an interface class

Izaservice z = (izaservice) context.getbean ("Zaservice");

And my business parameters are the full path name of the implementation class, so it is especially suitable for class.forname (); Then Bytype. It's a pity I can't use it. You can only go back and then adjust the parameters to the full path name of the interface. But there can only be one implementation class:

As to why the type of the cut class in the spring container has changed, it might be considered proxy reflection, and the personal view is that spring uses the JDK dynamic proxy by default, which generates a completely new class, such as the

 $Proxy 15, then code through this proxy class method before and after, and then call the method of the original class: Say far, .... 

Note: Spring also has a proxy method: CGLIB does not generate a new class, so if you do not generate a new class, you will not have the above problem, and now verify (Spring on the class that does not implement the interface proxy method is CGLIB)

We just have to Izaserviec remove on the line do not zaserivce realize it, try,,,,,, really normal.
@Servicepublic class Zaservice  {    @TestAnn    /* @Override    */public void AA () {    }} @ResponseBody    @RequestMapping ("/context") public    String context () {        ApplicationContext context =  Applicationcontextutils.context;        Zaservice z = (zaservice) Context.getbean (zaservice.class);        System.out.println (z);        Z.aa ();        Return "context";    } [Email protected]<>>>>>>>>>>>>>>>>>>>>>> >after<><><><><>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>

  



Spring's Applicationcontext.getbean (type) cannot get the bean, error

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.