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

 

 

时间: 2024-08-31 00:50:54

spring使用中报Cannot proxy target class because CGLIB2 is not available错的相关文章

spring aop报错:Cannot proxy target class because CGLIB2 is not available.

问题描述 今天在整合spring aop时报了这个错:Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpwordDa

task-使用spring自带的Task,放在服务器上启动时会报错

问题描述 使用spring自带的Task,放在服务器上启动时会报错 spring-mvc org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class Caused by: java.lang.TypeNotPresentException: Type java.lang.annotation.Repeatable not present at co

ecos-ECOS :target.ld:46: syntax error一直报错

问题描述 ECOS :target.ld:46: syntax error一直报错 Google 了一下说是有关C处理器的问题,但是...然并卵. 解决方案 看46行,语法错误.

【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合activeMQ,pom.xml文件缺架包,启动报错: [springDemo][INFO] [2017-12-11 14:54:57] org.springframework.web.context.ContextLoader.initWebApplicationContext(304) | Root

spring,struts2整合时web.xml中配置过滤器后部署报错

问题描述 <?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.s

配置Spring mvc 注解配置的时候 静态页面跳转 报的错

问题描述 <mvc:default-servlet-handler /> 相应的包 都加了, 就是报错, org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xm

Spring Aop开发过程中可能出现的异常(通过这些异常可以知道需要什么Jar包)

出现的问题解决: 问题1:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.util.ClassUtils.<clinit>(ClassUtils.java:67)   at org.springframework.core.io.DefaultResourceLoader.<in

Spring源代码解析(七):Spring AOP中对拦截器调用的实现

前面我们分析了Spring AOP实现中得到Proxy对象的过程,下面我们看看在Spring AOP 中拦截器链是怎样被调用的,也就是Proxy模式是怎样起作用的,或者说Spring是怎样为 我们提供AOP功能的: 在JdkDynamicAopProxy中生成Proxy对象的时候: Java代码 return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); 这里的this参数对应的是InvocationHandler对象

Spring AOP源码分析(八)SpringAOP要注意的地方

SpringAOP要注意的地方有很多,下面就举一个,之后想到了再列出来:  (1)SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法,另外不会对最外层的public方法内部调用的其他方法也进行拦截,即只停留于代理对象所调用的方法.如下案例:  B类有两个public方法,foo1()和foo2(),foo1内部调用了foo2,简单如下:  ? 1 2 3 4 5 6 7 8 public void foo2() {          Syste