二、biz接口
package org.aop.biz; /** * 在线图书销售系统业务逻辑接口 * * @author miao */ public interface BookBiz { /** * 买书的业务逻辑 * * @param userName * @param bookName * @param price * @return */ public boolean buy(String userName, String bookName, double price); /** * 发表书评的业务逻辑 * * @param userName * @param comments */ public void comment(String userName, String comments); }
三、bizImpl实现类
package org.aop.biz.impl; import org.aop.biz.BookBiz; /** * 在线图书销售系统业务逻辑接口实现类 * * @author miao * */ public class BookBizImpl implements BookBiz { /** * 购买图书 */ @Override public boolean buy(String userName, String bookName, double price) { System.out.println("------------------------"); System.out.println("·" + userName + "购买图书:" + bookName); System.out.println("·" + userName + "增加积分:" + (int) (price / 10)); System.out.println("------------------------"); return true; } /** * 发表书评 */ @Override public void comment(String userName, String comments) { System.out.println("------------------------"); System.out.println("·" + userName + "发表书评:" + comments); System.out.println("------------------------"); } }
四、前置/后置/环绕通知
前置:
package org.aop.log; import java.lang.reflect.Method; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import org.springframework.aop.MethodBeforeAdvice; /** * 前置通知 输出每个方法的参数,调用的时间 * * @author miao * */ public class LogAdvice implements MethodBeforeAdvice { private DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒sss毫秒"); /** * @param m 方法本身 * @param args 方法的参数 * @param target 调用此方法的对象 */ public void before(Method m, Object[] args, Object target) throws Throwable { System.out.println("\n[系统日志][" + sdf.format(new Date()) + "[调用方法:" + m.getName() + "(参数是:" + Arrays.toString(args) + ")调用的对象是:" + target); } }
后置:
package org.aop.log; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.aop.AfterReturningAdvice; /** * 后置通知 * * @author miao * */ public class RakeOffAdvice implements AfterReturningAdvice { /** * @param returnValue 返回值 * @param method 方法本身 * @param args 参数数组 * @param target 对象本身 */ public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { // 只有buy方法才有返利 if (method.getName().equals("buy")) { System.out.println("[销售返利][" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "]" + args[0] + ":返利5元哦亲!"); } } }
环绕:
package org.aop.log; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 环绕通知实例 * * @author miao * */ public class RoundAdvice implements MethodInterceptor { /** * MethodInterceptor不但封装目标方法及其入参数组,还封装了目标方法所在的实例对象 * 通过getArguments()可以获取目标犯法的入参数组,通过proceed()反射调用目标实例相应的方法 */ public Object invoke(MethodInvocation invocation) throws Throwable { if ("buy".equals(invocation.getMethod().getName())) { Object[] args = invocation.getArguments();// 目标方法入参 String userName = (String) args[0]; System.out.println("你好,欢迎光临!" + userName);// 运行方法前调用 boolean obj = (Boolean) invocation.proceed();// 调用方法,obj方法的返回值 if (obj) { System.out.println("谢谢惠顾!");// 目标方法之后运行 } else { System.out.println("购买失败!");// 目标方法之后运行 } return obj; } return null; } }
五、spring的配置文件(即前置/后置/环绕通知的配置)
前置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 真正的业务实现类 --> <bean id="bookBizTarget" class="org.aop.biz.impl.BookBizImpl"/> <!-- 前置通知类 --> <bean id="logAdvice" class="org.aop.log.LogAdvice"/> <!-- 代理类,开始织入 --> <bean id="bookBiz" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 相应的业务接口 --> <property name="proxyInterfaces"> <list> <value>org.aop.biz.BookBiz</value> </list> </property> <!-- 切面类 --> <property name="interceptorNames"> <list> <value>logAdvice</value> </list> </property> <!-- 代理的实现类 --> <property name="targetName"> <value>bookBizTarget</value> </property> </bean> </beans>
后置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 后置通知类 --> <bean id="rakeOffAdvice" class="org.aop.log.RakeOffAdvice" /> <!-- 书籍的业务逻辑实现类 --> <bean id="bookBiz" class="org.aop.biz.impl.BookBizImpl"/> <!-- 通过ID自动代理工厂类 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>*Biz</value> </property> <property name="interceptorNames"> <list> <value>rakeOffAdvice</value> </list> </property> </bean> </beans>
环绕:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 真正的业务实现类 --> <bean id="bookBizTarget" class="org.aop.biz.impl.BookBizImpl" /> <!-- 前置通知类 --> <bean id="roundAdvice" class="org.aop.log.RoundAdvice" /> <!-- 代理类,开始织入 --> <bean id="bookBiz" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 相应的业务接口 --> <property name="proxyInterfaces"> <list> <value>org.aop.biz.BookBiz</value> </list> </property> <!-- 切面类 --> <property name="interceptorNames"> <list> <value>roundAdvice</value> </list> </property> <!-- 代理的实现类 --> <property name="target" ref="bookBizTarget" /> </bean> </beans>
七、测试类:
package org.aop.test; import org.aop.biz.BookBiz; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试类 * * @author miao * */ public class AopTest { /** * 测试类,调用代理的工厂类 * * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("aopBefore.xml"); //ApplicationContext context = new ClassPathXmlApplicationContext("aopRound.xml"); //ApplicationContext context = new ClassPathXmlApplicationContext("aopAfter.xml"); // 将代理工厂作为业务接口的子对象 BookBiz bookBiz = (BookBiz) context.getBean("bookBiz"); // 直接调用接口方法 bookBiz.buy("小伙伴", "暴走漫画", 100); bookBiz.comment("王蜜桃", "不要在意这些细节"); } }
八、demo