1、问题描述:拦截所有业务方法,判断用户是否有权限,有权限就允许他执行业务方法。
1.1定义一个UserServcie业务接口
public interface UserService { public void save(); public void update(String name,Integer userid); public String getUserName(Integer userid); }
1.2、编写实现UserServcie接口的类
public class UserServiceBean implements UserService { private String user = null; public UserServiceBean (String user){ this.user = user; } public UserServiceBean (){ } public String getUserName(Integer userid) { return "xxx"; } public String getUser() { return user; } public void save() { // if(user!=null){System.out.println("我是save()方法");} System.out.println("我是save()方法"); } public void update(String name, Integer userid) { System.out.println("我是update()方法"); } }
2、编写JDKProxyFactory代理工厂类,要求实现java.lang.reflect.InvocationHandler接口,实现invoke方法.
public class JDKProxyFactory implements InvocationHandler{ private Object targetObject; //创建目标对象的代理类 public Object createProxyInstance(Object targetObject){ this.targetObject = targetObject; return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), //實現目標對象的所有接口,InvocationHandler是回調 this.targetObject.getClass().getInterfaces(), this); } //當客戶端調用代理對象方法的時候,會先調用this的這個方法invoke public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { PersonServiceBean bean = (PersonServiceBean)this.targetObject; Object result = null; if(bean.getUser()!=null){ //最終方法的調用委派給目標對象targetObject result = method.invoke(targetObject, args); }else{ System.out.println("沒有權限,退出程序....."); } return result; } }
2.2 第二种方式,创建 CGlibProxyFactory代理工厂,要求实现MethodInterceptor接口,并实现其中的intercept方法.
(注:要加入cglib相关jar包)
public class CGlibProxyFactory implements MethodInterceptor{ private Object targetObject; public Object createProxyInstance(Object targetObject){ this.targetObject = targetObject; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(this.targetObject.getClass());//继承目标类,重写final方法 enhancer.setCallback(this); return enhancer.create(); } //类似struts2拦截器 public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { UserServiceBean bean = (UserServiceBean)this.targetObject; Object result = null; if(bean.getUser()!=null){ result = method.invoke(targetObject, args); }else{ System.out.println("沒有權限,退出程序....."); } return result; } }
3、编写测试类
public class AOPTest extends TestCase { @Test public void proxyTest(){ //测试JDKProxy System.out.println("----------测试JDKProxy----------"); JDKProxyFactory factory = new JDKProxyFactory(); UserService userService = (UserService )factory.createProxyInstance(new UserServiceBean()); userService .save(); userService = (UserService )factory.createProxyInstance(new UserServiceBean("test")); userService .save(); //测试CGlibProxy System.out.println("----------测试CGlibProxy----------"); CGlibProxyFactory factory2 = new CGlibProxyFactory(); userService = (UserServiceBean)factory2.createProxyInstance(new PersonServiceBean()); userService .save(); userService = (UserServiceBean)factory2.createProxyInstance(new UserServiceBean("test")); userService .save(); } }
运行结果:
----------测试JDKProxy---------- 沒有權限,退出程序..... 我是save()方法 ----------测试CGlibProxy---------- 沒有權限,退出程序..... 我是save()方法