public class User { private Integer id; private String name; }
public class UserDao { public void save(User user){ System.out.println("save user...."); } public void delete(int id){ System.out.println("delete user...."); } public void update(User user) { System.out.println("update user ...."); } public User query(String name) { System.out.println("getUser ...."); return new User(); } }
public class UserBeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) { System.out.println("调用方法:"+method.getName() + "()前拦截处理"); } }
public class UserAfterAdvice implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) { System.out.println("方法:"+method.getName() + "()返回后拦截处理"); } }
public class UserAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("调用方法:"+invocation.getMethod().getName() + "()前拦截处理"); Object o = invocation.proceed(); System.out.println("调用方法:"+invocation.getMethod().getName() + "()后拦截处理"); return o; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="userDaoTarget" class="com.boya.spring.dao.UserDao" /> <bean id="userBeforeAdvice" class="com.boya.spring.aop.UserBeforeAdvice" /> <bean id="userAfterAdvice" class="com.boya.spring.aop.UserAfterAdvice" /> <bean id="userAroundAdvice" class="com.boya.spring.aop.UserAroundAdvice" /> <bean id="userDao" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list><value>userAroundAdvice</value></list> </property> <property name="target" ref="userDaoTarget"></property> </bean> </beans>
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserDao userDao = context.getBean("userDao", UserDao.class); userDao.save(new User()); }
调用方法:save()前拦截处理save user....调用方法:save()后拦截处理
public interface SignInterface { public Object sign(String nameList); }
public class Teacher implements SignInterface { public Object sign(String nameList) { System. out .println( "Teacher sign..." ); return new Object(); } }
public class Leader implements SignInterface { private Teacher teacher; public Object sign(String nameList) { if (teacher == null) { teacher = new Teacher(); } Object o = teacher.sign(nameList); return o; } }
public static void main(String[] args) { SignInterface s = new Leader(); s.sign("names"); }
public class ProxyObject implements InvocationHandler { private Object proxy_obj; ProxyObject(Object obj) { this.proxy_obj = obj; } public static Object getProxy(Object obj) { Class cls = obj.getClass(); // 通过Proxy类的newProxyInstance方法来返回代理对象 return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new ProxyObject(obj)); } /** * 实现InvocationHandler接口的invoke */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("调用方法:" + method + "()之前拦截处理"); if (args != null) { System.out.println("方法有" + args.length + "个参数"); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } // 利用反射机制动态调用真实对象的方法 Object o = method.invoke(proxy_obj, args); System.out.println("调用方法:" + method + "()之后拦截处理"); return o; } // 测试代码 public static void main(String agr[]) { SignInterface si = (SignInterface) getProxy(new Teacher()); si.sign("names"); } }
public class CGLibTeacher { public Object sign(String nameList) { System.out.println("Teacher sign..."); return new Object(); } }
public class CGLibAop implements MethodInterceptor { public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println("before..."); Object o = arg3.invokeSuper(arg0, arg2); System.out.println("after..."); return o; } }
public class CGLibProxy { public static CGLibTeacher create(CGLibAop aop){ Enhancer en = new Enhancer(); //进行代理 en.setSuperclass(CGLibTeacher.class); en.setCallback(aop); //生成代理实例 return (CGLibTeacher)en.create(); } public static void main(String[] args) { CGLibTeacher t = CGLibProxy.create(new CGLibAop()); t.sign("names"); } }
Spring3.x教程(二) Spring AOP,布布扣,bubuko.com
原文地址:http://blog.csdn.net/boyazuo/article/details/38128591