代理类伪代码
public class BookBizImpl implements IBookBiz{} public class $Proxy4 implements IBookBiz{ IBookBiz target; String[] interceptorNames = new String[]{"myMethodBeforeAdvice"}; buy(a,b,c){ myMethodBeforeAdvice.before(....); target.buy(a,b,c); myAfterReturningAdvice.afterReturning(....); } comment(){ } } IBookBiz BookBizImple $Proxy0 bookBiz.buy("张三", "西游记", 88d); $Proxy0.buy("张三", "西游记", 88d)
package com.javaxl.aop.exception; public class PriceException extends RuntimeException { public PriceException() { super(); } public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public PriceException(String message, Throwable cause) { super(message, cause); } public PriceException(String message) { super(message); } public PriceException(Throwable cause) { super(cause); } }
package com.javaxl.aop.biz.impl; import com.javaxl.aop.biz.IBookBiz; import com.javaxl.aop.exception.PriceException; public class BookBizImpl implements IBookBiz { public BookBizImpl() { super(); } public boolean buy(String userName, String bookName, Double price) { // 通过控制台的输出方式模拟购书 if (null == price || price <= 0) { throw new PriceException("book price exception"); } System.out.println(userName + " buy " + bookName + ", spend " + price); return true; } public void comment(String userName, String comments) { // 通过控制台的输出方式模拟发表书评 System.out.println(userName + " say:" + comments); } }
package com.javaxl.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.MethodBeforeAdvice; /** * 调用项目中的某一接口实现类的方式时,将类名.方法名,携带的参数,作为日志存储到数据库。 * @author Administrator * */ public class MyMethodBeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable { String targetName = target.getClass().getName(); String methodName = method.getName(); String params = Arrays.toString(args); String msg = "【系统日志】:正在调用->"+targetName+"."+methodName+",携带的参数:"+params; System.out.println(msg); } }
package com.javaxl.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.AfterReturningAdvice; /** * 后置通知 * @author Administrator * */ public class MyAfterReturningAdvice implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { String targetName = target.getClass().getName(); String methodName = method.getName(); String params = Arrays.toString(args); String msg = "【返利通知:返利3元】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params + ";目标对象所调用的方法的返回值:" + returnValue; System.out.println(msg); } }
package com.javaxl.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 环绕通知 * @author Administrator * */ public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { Object target = invocation.getThis(); Method method = invocation.getMethod(); Object[] args = invocation.getArguments(); // a.jsp window.open(b.jsp) // b.jsp xxx->返回object->b.jsp window.close();window.getArguments; String targetName = target.getClass().getName(); String methodName = method.getName(); String params = Arrays.toString(args); String msg = "【环绕通知】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params; System.out.println(msg); Object returnValue = invocation.proceed(); String msg2 = "【环绕通知】:目标对象所调用的方法的返回值:" + returnValue; System.out.println(msg2); return returnValue; } }
package com.javaxl.aop.advice; import org.springframework.aop.ThrowsAdvice; import com.javaxl.aop.exception.PriceException; /** * 异常通知 * @author Administrator * */ public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing( PriceException ex ) { System.out.println("价格输入有误,购买失败,请重新输入!!!"); } }
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" default-autowire="byType" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="userBiz1" class="com.javaxl.ioc.biz.impl.UserBizImpl1"></bean> <bean id="userBiz2" class="com.javaxl.ioc.biz.impl.UserBizImpl2"></bean> <!-- IOC注入 --> <bean id="userAction" class="com.javaxl.ioc.web.UserAction"> <!-- <property name="sid" value="22"></property> --> <!-- <property name="sname" value="zs"></property> --> <constructor-arg name="sid" value="22"></constructor-arg> <constructor-arg name="sname" value="zs"></constructor-arg> <property name="hobby"> <list> <value>篮球</value> <value>足球</value> <value>读书</value> </list> </property> <!-- 项目中的Bean交给spring进行管理,由原来的程序员维护,变成spring维护,方便项目的后期维护,这就是控制反转 --> <!-- <property name="userBiz" ref="userBiz2"></property> --> </bean> <!-- ************************aop*********************** --> <!-- 目标对象 --> <bean class="com.javaxl.aop.biz.impl.BookBizImpl" id="bookBiz"></bean> <!-- 前置通知 --> <bean class="com.javaxl.aop.advice.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean> <!-- 后置通知 --> <bean class="com.javaxl.aop.advice.MyAfterReturningAdvice" id="myAfterReturningAdvice"></bean> <!-- 环绕通知 --> <bean class="com.javaxl.aop.advice.MyMethodInterceptor" id="myMethodInterceptor"></bean> <!-- 异常通知 --> <bean class="com.javaxl.aop.advice.MyThrowsAdvice" id="myThrowsAdvice"></bean> <!-- 过滤通知 --> <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterReturningAdvice2"> <property name="advice" ref="myAfterReturningAdvice"></property> <!-- .*buy:[.*]任意字符0~n个,以buy结尾的方法 --> <!-- <property name="pattern" value=".*buy"></property> --> <property name="patterns"> <list> <value>.*buy</value> </list> </property> </bean> <!-- 生成代理(目标对象+通知) --> <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="proxyFactoryBean"> <property name="target" ref="bookBiz"></property> <!-- 代理工厂生产的代理需要实现的接口列表 --> <property name="proxyInterfaces"> <list> <value>com.javaxl.aop.biz.IBookBiz</value> </list> </property> <property name="interceptorNames"> <list> <value>myMethodBeforeAdvice</value> <!-- <value>myAfterReturningAdvice</value> --> <value>myAfterReturningAdvice2</value> <value>myMethodInterceptor</value> <value>myThrowsAdvice</value> </list> </property> </bean> </beans>
测试代码及执行结果如下
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有