[Solved] Spring Set Error

When using spring’s dependency injection (annotation or XML mode) and AOP function in spring 3 or above, we found a problem; aop:aspectj-autoproxy proxy-target-class=”true”/> Then, when you get the bean, you always report: (no bean can be obtained by name or type)

1 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
2 AccountService accountService = app.getBean(AccountServiceImpl.class);//Here, the bean is obtained through the implementation class of the AccountService interface

The error information is as follows:

1 org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type
2 or
3 java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to cn.edu.nuc.SpringTest.service.impl.DemoServiceImpl

The difference between: proxy-target-class=”true” and proxy-target-class=”false”

The proxy-target-class attribute value determines whether an interface-based or class-based proxy is created.

  <aop:aspectj-autoproxy proxy-target-class=”false”/>Interface-based, using JDK dynamic proxies

  <aop:aspectj-autoproxy proxy-target-class=”true”/>Class-based, requires the use of the cglib library

Similar Posts: