天天看點

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接口

public class studentserviceimpl

在使用的時候會報cannot proxy target class because cglib2 is not available

問題原因

代理為控制要通路的目标對象提供了一種途徑。當通路對象時,它引入了一個間接的層。jdk自從1.3版本開始,就引入了動态代理,并且經常被用來動态地建立代理。jdk的動态代理用起來非常簡單,但它有一個限制,就是使用動态代理的對象必須實作一個或多個接口。

解決辦法

方案一 使實際的類實作某個接口

方案二 使用cglib包

cglib是一個開源項,一個強大的,高性能,高品質的code生成類庫,它可以在運作期擴充java類與實作java接口。hibernate用它來實作po位元組碼的動态生成。