测试发现新的代理类生成模块不支持泛型,cglib是支持的;可以ProxyManager.me().setProxyFactory(new CglibProxyFactory());改为cglib的方式;
package jfdemo;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class EmptyInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
inv.invoke();
}
}
package jfdemo;
import com.jfinal.aop.Before;
public class A<T> {
private T obj;
@Before(EmptyInterceptor.class)
public T getObj() { // 泛型不支持
return obj;
}
@Before(EmptyInterceptor.class)
public void setObj(T obj) { // 泛型不支持
this.obj = obj;
}
}
package jfdemo;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Before;
import com.jfinal.ext.proxy.CglibProxyFactory;
import com.jfinal.proxy.ProxyManager;
public class B extends A<String> {
@Before(EmptyInterceptor.class)
public void show() {
System.out.println(getObj());
}
public static void main(String[] args) {
//ProxyManager.me().setProxyFactory(new CglibProxyFactory());
B b = Aop.get(B.class);
b.setObj("aaa");
System.out.println(b.getClass().getName());
b.show();
}
}
生成的代码如下:
package jfdemo;
import com.jfinal.aop.Invocation;
public class B$$EnhancerByJFinal extends B {
public void show() {
Invocation inv = new Invocation(this, 1L,
args -> {
B$$EnhancerByJFinal.super.show(
);
return null;
}
);
inv.invoke();
}
public T getObj() {
Invocation inv = new Invocation(this, 2L,
args -> {
return B$$EnhancerByJFinal.super.getObj(
);
}
);
inv.invoke();
return inv.getReturnValue();
}
public void setObj(T p0) {
Invocation inv = new Invocation(this, 3L,
args -> {
B$$EnhancerByJFinal.super.setObj(
(T)args[0]
);
return null;
}
, p0);
inv.invoke();
}
}
建议通过配置 me.setProxyFactory(new CglibProxyFactory()) 解决,这个方案在性能上是完全一样的,十分好用