spring使用中报Cannot proxy target class because CGLIB2 is not available错

简介:

发现问题

public interface StudentService
{
 void add();
}

@Service
public class StudentServiceImpl implements StudentService
{
 void add(){  }
}

public class StudentAction extends ActionSupport
{
 private StudentService studentService;

 @Resource
 public void setStudentService()
 {

 }
}

以上描述了一个很简单的注入过程。但若StudentServiceImpl没有实现StudentService接口

@Service
public class StudentServiceImpl
{
 void add(){  }
}

在使用的时候会报Cannot proxy target class because CGLIB2 is not available

 

 

问题原因

代理为控制要访问的目标对象提供了一种途径。当访问对象时,它引入了一个间接的层。JDK自从1.3版本开始,就引入了动态代理,并且经常被用来动态地创建代理。JDK的动态代理用起来非常简单,但它有一个限制,就是使用动态代理的对象必须实现一个或多个接口。

 


解决办法

方案一 使实际的类实现某个接口
方案二 使用CGLIB包

cglib是一个开源项,一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java类与实现Java接口。Hibernate用它来实现PO字节码的动态生成。

 

参考地址:http://baike.baidu.com/view/1254036.htm

本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/973416如需转载请自行联系原作者


woshixuye111

相关文章
|
3月前
|
缓存 负载均衡 Java
Spring Cloud Alibaba client升级问题之升级报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
7月前
|
XML Java 数据格式
如果Spring中有两个ID相同的Bean,会报错吗?
有位粉丝被 问到这样一个问题,说在Spring中,如果有两个ID相同的Bean,会不会报错?如果报错,会在哪个阶段报错? 这个问题也要分析具体的情况,才能完整的回答。我从三个方面来回答你的问题吧。
95 0
|
4月前
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
255 0
|
4月前
|
前端开发 Java 数据库连接
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
|
30天前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
15 0
|
30天前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
8 0
|
30天前
|
前端开发 Java Spring
ssm中spring mvc找不到控制器,报错404
ssm中spring mvc找不到控制器,报错404
14 0
|
6月前
|
Java 数据库连接 mybatis
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
128 0
|
2月前
|
Java Spring
spring-doc报错Unable to render this definition
spring-doc报错Unable to render this definition
155 0
|
2月前
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
37 0