admin管理员组文章数量:1568418
spring boot 项目运行报错:
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxService' is expected to be of type 'com.yangxd.application.service.xxService' but was actually of type 'com.sun.proxy.$Proxy48'
解决办法:启动类增加 @EnableAspectJAutoProxy(proxyTargetClass = true)
注解即可。
阅读以下源码很容易看出,如果目标类是接口或者代理类,使用 jdk 动态代理,否则使用 cglib 代理。
在 spring boot 启动类中添加 @EnableAspectJAutoProxy(proxyTargetClass = true)
,会激活 CglibAutoProxyConfiguration,启用 cglib 代理。
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
Class<?>[] ifcs = config.getProxiedInterfaces();
return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
}
}
本文标签: 报错expectedSpringboottype
版权声明:本文标题:spring boot 报错 : expected to be of type 'xxService' but was actually of type 'com.sun.proxy.$Proxy48 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726602678a1077222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论