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

 

 

目录
相关文章
|
设计模式 Java 开发者
【小家Spring】面向切面编程之---Spring AOP的原理讲解以及源码分析(Cannot find current proxy: Set 'exposeProxy' property on )(下)
【小家Spring】面向切面编程之---Spring AOP的原理讲解以及源码分析(Cannot find current proxy: Set 'exposeProxy' property on )(下)
【小家Spring】面向切面编程之---Spring AOP的原理讲解以及源码分析(Cannot find current proxy: Set 'exposeProxy' property on )(下)
|
3月前
|
XML 缓存 Java
Spring5源码(7)-lookup-method和replace-method注入
Spring5源码(7)-lookup-method和replace-method注入
21 0
|
3月前
|
Java Spring
spring框架 aop:aspectj-autoproxy proxy-target-class=“true“用法理解
spring框架 aop:aspectj-autoproxy proxy-target-class=“true“用法理解
|
4月前
|
Java Spring
java Proxy 解析---spring aop
java Proxy 解析---spring aop
27 0
|
4月前
|
Java 数据库连接 API
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
85 0
|
9月前
|
XML SpringCloudAlibaba Java
Spring注解配置:@Configuration 和 @Component 区别及原理详解
随着`Spring Boot`的盛行,注解配置式开发受到了大家的青睐,从此告别了基于`Spring`开发的繁琐`XML`配置。这里先来提纲挈领的了解一下`Spring`内部对于配置注解的定义,如`@Component、@Configuration、@Bean、@Import`等注解,从功能上来讲,这些注解所负责的功能的确不相同,但是
178 1
|
Java Spring
spring boot Configuration Annotation Proessor not found in classpath(贼简单)
spring boot Configuration Annotation Proessor not found in classpath(贼简单)
|
XML Java 数据库连接
【异常】Mybatis中,SpringMVC中,SpringBoot项目中,出现There is no getter for property named 'xxxxxx'报错的解决方法
Mybatis中,SpringMVC中,SpringBoot项目中,出现There is no getter for property named 'xxxxxx'报错的解决方法
229 0
|
Java 应用服务中间件 Maven
SpringBoot启动报错:Failed to introspect Class [XXX] from ClassLoader解决办法
SpringBoot启动报错:Failed to introspect Class [XXX] from ClassLoader解决办法
1428 0
SpringBoot启动报错:Failed to introspect Class [XXX] from ClassLoader解决办法
|
Java 数据库连接 Maven
【Spring常见错误】idea添加@Data注解后无getter&setter提示
📋📋 精彩摘要:MyBatis 核心配置文件(xxxConfig.xml),该文件配置了MyBatis的一些全局信息,,包含数据库连接信息和MyBatis运行时所需的各种特性,以及设置和响应MyBatis行为的一些属性。本文将深入浅出的介绍MyBatis核心配置文件中常用的标签配置。
356 0