Java中的一些代理技术
使用cglib,asm 对接口进行拦截,这里需要调用Invoke方法
final IUserService userService=new UserService(); Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(IUserService.class); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println(arg1.getName()); return arg3.invoke(userService, arg2); } }); IUserService proxy= (IUserService) enhancer.create(); System.out.println(proxy.getNameById(3));
final IUserService userService=new UserService(); Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(UserService.class); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println(arg1.getName()); return arg3.invoke(userService, arg2); } }); IUserService proxy= (IUserService) enhancer.create(); System.out.println(proxy.getNameById(3));
拦截一些自开发类(自己控制类的创建),非其他框架生成的,就不需要代理中引用的Target了
Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(UserService.class); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println(arg1.getName()); return arg3.invokeSuper(arg0, arg2); } }); IUserService proxy= (IUserService) enhancer.create(); System.out.println(proxy.getNameById(3));
使用动态代理,只支持接口
final IUserService userService=new UserService(); IUserService proxy2=(IUserService) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{ IUserService.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println(method.getName()); return method.invoke(userService, args); } });
使用Enhancer与动态代理,拦截Request,注意Enhancer需要设置类加载器
package cn.haoda.web.filter; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.haoda.web.filter.imp.UserService; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * Servlet Filter implementation class Filter1 */ public class Filter1 implements Filter { /** * Default constructor. */ public Filter1() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(final ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here // pass the request along the filter chain final HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(HttpServletRequest.class); enhancer.setClassLoader(this.getClass().getClassLoader()); //需要指定这句不然会报错 enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println("Enhancer->" +arg1.getName()); return arg3.invoke(request, arg2); } }); ServletRequest proxyReq=(ServletRequest) Proxy.newProxyInstance(this.getClass().getClassLoader(), request.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("动态代理->" +method.getName()); return method.invoke(request, args); } }); chain.doFilter((HttpServletRequest)enhancer.create(), response); //chain.doFilter(proxyReq, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
参考:
http://llying.iteye.com/blog/220452
http://stackoverflow.com/questions/11574049/cglib-with-spring-throws-illegalaccesserror
关于类加载器:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/