发现问题
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
分享到:
相关推荐
关于Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.和 java.lang.ClassNotFoundException: org.objectweb.asm.Type错误的解决方法: 就是...
org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces 从异常信息可以看出是cglib的问题...
主要介绍了Spring如何基于Proxy及cglib实现动态代理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
静态代理--不适合企业开发,适合初学者理解代理。 jdk动态代理--适合企业级开发,但是它要求必须面向接口编程,假如目标类没有实现接口...spring 的AOP功能中 会根据目标类是否实现了接口来判断使用 jdk Proxy还是cglib
spring proxy代理模仿 提练出来了
Spring源代码解析(五):Spring_AOP获取Proxy.doc
nginx: [emerg] “proxy_cache_path” directive is not allowed here in /etc/nginx/conf.d/default.conf:29 提示意思“proxy_cache_path指令不被允许”,在官网上查找了相关说明,也没有发现问题,最后看应用范围...
使用JDK中的Proxy技术实现AOP功能与使用CGLIB实现AOP功能
aop:aspectj-autoproxy proxy-target-class="true"/> JDK动态代理和CGLIB字节码生成的区别? * JDK动态代理只能对实现了接口的类生成代理,而不能针对类 * CGLIB是针对类实现代理,主要是对指定的类生成一个...
public class Client { public static void main(String[] args) { // UserService userService=new UserServiceImpl(); // ProxyInovationHandler pih =new ProxyInovationHandler(); // pih.setTarget...
Spring示例_Printer_Spring_AOP_Spring_Proxy
Spring初探 准备工作 构建Spring基础代码 Spring 基础语义 Dependency Injection 依赖注入的几种实现类型 ... Dynamic Proxy 与Spring AOP CGLib 与 Spring AOP AOP 应用 DAO Support Remoting
在java编程使用CGLIB做动态代理时需要CGLIB依赖包支持,没有这个支持包,当导入import net.sf.cglib.proxy.Enhancer;或者import net.sf.cglib.proxy.MethodProxy;类似包时会提示错误,所以分享出来同大家共勉。
SpringIOC,DI+dynamic proxy 实现盗版AOP的源代码
有一天在用回调模版的时候,忽然间又想到jdk自带的Proxy类似乎不是怎么好用,为何不把其做成回调模版呢,可以将其改造称类似spring Aop 代码如下: package com.fjx.proxy.up.test; import ...
* 使用cglib动态代理 * @author * */ public class BookFacadeProxy implements MethodInterceptor{ private Object target; @Override public Object intercept(Object obj, Method method, Object[] ...
使用SpringCloud Zuul Proxy构建API网关.docx
spring AOP中的cglib代理所依赖的jar包cglib.jar源码 net.sf.cglib.proxy
spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 04_Spring的三种实例化Bean的方式 ...使用Spring集成Struts2 34.所有源码