AOP中关键性概念
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>T226_huang</groupId> <artifactId>T226_spring2_aop</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>T226_spring2_aop Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.10.RELEASE</version> </dependency> </dependencies> <build> <finalName>T226_spring2_aop</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
工具类:
IbookBiz
package com.huang.aop.biz; public interface IBookBiz { // 购书 public boolean buy(String userName, String bookName, Double price); // 发表书评 public void comment(String userName, String comments); }
BookBizimpl
package com.huang.aop.biz.impl; import com.huang.aop.biz.IBookBiz; import com.huang.aop.ex.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.huang.aop.ex; 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); } }
前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志
package com.huang.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.MethodBeforeAdvice; /** * 买书、评论前加系统日志 * @author Admin * */ public class MyMethodBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { String clzName=target.getClass().getName(); String methodName=method.getName(); String params=Arrays.toString(args); System.out.println("[买书、评论前加系统日志]:"+clzName+"."+methodName+"("+params+")"); } }
后置通知
实现org.springframework.aop.AfterReturningAdvice接口
买书返利(存在bug)
package com.huang.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.AfterReturningAdvice; /** * 买书返利(存在bug) * @author Admin * */ public class MyAfterReturningAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { String clzName=target.getClass().getName(); String methodName=method.getName(); String params=Arrays.toString(args); System.out.println("[买书返利的后置通知]:"+clzName+"."+methodName+"("+params+")"+"\t 目标对象方法调用后的返回值"+returnValue); } }
环绕通知
org.aopalliance.intercept.MethodInterceptor
类似拦截器,会包括切入点,目标类前后都会执行代码
package com.huang.aop.advice; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { String clzName=invocation.getThis().getClass().getName(); String methodName=invocation.getMethod().getName(); String params=Arrays.toString(invocation.getArguments()); System.out.println("【环绕通知】:"+clzName+"."+methodName+"("+params+")"); Object returnValue=invocation.proceed(); System.out.println("【环绕通知】\t目标对象方法调用后的返回值"+returnValue); return returnValue; } }
异常通知
org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
package com.huang.aop.advice; import org.springframework.aop.ThrowsAdvice; import com.huang.aop.ex.PriceException; /** * 异常通知 * @author Administrator * 案例:张三向李四转账 * biz.transfer(user1,user2) * UserDao.update(user1) * UserDao.update(user2) */ public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing( PriceException ex ) { System.out.println("价格输入有误,购买失败,请重新输入!!!"); } }
过滤通知(适配器)
org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
<?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" default-autowire="byType" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean class="com.huang.ioc.biz.impl.UserBizImpl2" id="userBiz"></bean> <bean class="com.huang.ioc.web.UserAction" id="xxx"> <!-- 构造注入用constructor-arg标签 --> <constructor-arg name="uname" value="ls"></constructor-arg> <constructor-arg name="age" value="22"></constructor-arg> <property name="hobby" > <list> <value>充钱使你强大</value> <value>RMB</value> <value>腾讯</value> </list> </property> </bean> <bean class="com.huang.ioc.web.OrderAction" id="ttt"> <!-- <property name="userBiz" ref="userBiz"></property> --> </bean> <!-- AOP --> <!-- 目标对象 --> <bean id="bookBiz" class="com.huang.aop.biz.impl.BookBizImpl"></bean> <!-- 通知 --> <bean id="myBefore" class="com.huang.aop.advice.MyMethodBeforeAdvice"></bean> <bean id="myAfter" class="com.huang.aop.advice.MyAfterReturningAdvice"></bean> <bean id="myInterceptor" class="com.huang.aop.advice.MyMethodInterceptor"></bean> <bean id="myThrowsAdvice" class="com.huang.aop.advice.MyThrowsAdvice"></bean> <bean id="myAfter2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAfter"></property> <property name="pattern" value=".*bug"></property> </bean> <!-- 有代 理工厂来组装目标对象及通知 --> <bean id="bookProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="bookBiz"></property> <property name="proxyInterfaces"> <list> <value>com.huang.aop.biz.IBookBiz</value> </list> </property> <property name="interceptorNames"> <list> <value>myBefore</value> <!-- <value>myAfter</value> --> <value>myAfter2</value> <value>myInterceptor</value> <value>myThrowsAdvice</value> </list> </property> </bean> </beans>
运行结果:
原文:https://www.cnblogs.com/bf6rc9qu/p/11355536.html