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:
- [Solved] The bean ‘sysDictService’ could not be injected because it is a JDK dynamic proxy
- Solution to the problem of consider defining a bean of type ‘xxx’ in your configuration in spring boot auto injection
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.southwind.mmall002.service.Impl.ProductCategoryServiceImplTest’:
- Transaction rolled back because it has been marked as rollback-only
- Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userDaoImpl’ defined in file
- Exception in thread “main” java.lang.ClassCastException
- [Solved] Springboot Error: Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
- dynamic_cast Error: source type is not polymorphic
- Proxy error: Could not proxy request [How to Solve]
- [Solved] Import AOP Error: error at ::0 formal unbound in pointcut