Bean named ‘XXX’ is expected to be of type [com.***.XXX] but was actually of type [com.sun.proxy.$Pr…

spring project

When running the local testcase, it was found that the program could not be started and the following error was reported:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bossAccAndTransModifyController': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'TPlatOrderServiceImpl':
Unsatisfied dependency expressed through field 'tLimitAddService':
Bean named 'TLimitAddServiceImpl' is expected to be of type [com.cn.yft.ora.service.impl.TLimitAddServiceImpl]
but was actually of type [com.sun.proxy.$Proxy43];
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'TLimitAddServiceImpl' is expected to be of type [com.cn.yft.ora.service.impl.TLimitAddServiceImpl]
but was actually of type [com.sun.proxy.$Proxy43]
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
...

Let’s look at the nest exception information:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘TLimitAddServiceImpl’ is expected to be of type [com.cn.yft.ora.service.impl.TLimitAddServiceImpl] but was actually of type [com.sun.proxy. $Proxy43]

The reason for this error is that spring’s declarative transaction annotation @Transactional is used on the method.

Need to be specified in the spring application context.xml file

xml header introduction

xmlns:aop=”http://www.springframework.org/schema/aop”

Next, I ran testcase and found that it still failed to start and a new error occurred:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 26 in XML document from class path resource [spring-mvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 26; columnNumber : 55; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    ...

Let’s look at the nest exception information:

The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.

The reason is that schemaLocation lacks AOP-related XSD configuration, so add AOP configuration in the XML file.

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

Next, I ran testcase and found that it still failed to start and a new error occurred:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    ...

or

java.lang.IllegalStateException: Failed to load ApplicationContext
\t
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bossAccAndTransModifyController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' TAccTransServiceImpl' defined in file [D:\***\tax_pay_ext\tax-pay-ext-trans\trans-core\target\classes\com\cn\yft\ora\ \service\impl\TAccTransServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
...

We see that the nest exception information is all java.lang.NoClassDefFoundError:

nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around<br>nested exception is java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable

NoClassDefFoundError, as the name suggests, is the lack of relevant dependencies. In addition to spring-aop dependencies, aspectj dependencies are also missing.

 <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>

ref: https://www.cnblogs.com/aeolian/p/11592398.html