目录
被通知(被代理)的对象
在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
将通知应用到目标对象后创建的对象(代理=目标+通知),只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器=通知(Advice)+切入点(Pointcut)
程序执行过程中明确的点,如方法的调用,或者异常的抛出.
BookBiz.java:
- package com.ruojuan.aop.biz;
-
- public interface BookBiz {
- // 购书
- public boolean buy(String userName, String bookName, Double price);
-
- // 发表书评
- public void comment(String userName, String comments);
- }
BookBizImpl.java:
- package com.ruojuan.aop.biz.impl;
-
- import com.ruojuan.aop.biz.BookBiz;
- import com.ruojuan.aop.exception.PriceException;
-
- public class BookBizImpl implements BookBiz {
-
- public BookBizImpl() {
- super();
- }
-
- public boolean buy(String userName, String bookName, Double price) {
- // 通过控制台的输出方式模拟购书
- if (null == price || price <= 0) {
- throw new PriceException("book price exception");
- }
- //logDao.add->sout("买书相关日志")
- System.out.println(userName + " buy " + bookName + ", spend " + price);
- return true;
- }
-
- public void comment(String userName, String comments) {
- // 通过控制台的输出方式模拟发表书评
- System.out.println(userName + " say:" + comments);
- //System.out.println("买书返利");
- }
-
- }
PriceException.java:
- package com.ruojuan.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);
- }
-
- }
在连接点之前执行的通知()
创建目标对象:
编写前置:
配置前置通知:
代理对象:
MyMethodBeforeAdvice.java
- package com.ruojuan.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.MethodBeforeAdvice;
-
- /**
- * 前置通知
- * 买书,评论前加系统日志
- * @author ruojuan
- * 名称:
- * 时间:2022年8月7日 下午1:59:14
- */
-
- public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
-
- @Override
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
- //目标对象的类名
- String clzName = arg2.getClass().getName();
- //当前调用的方法
- String methodName = arg0.getName();
- //当前调用方法所传递参数
- String args = Arrays.toString(arg1);
- System.out.println("【系统日志】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args);
-
- }
-
-
- }
spring-context.xml
-
- <bean class="com.ruojuan.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.ruojuan.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.ruojuan.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- <value>myAfterPlusvalue>
- <value>myMethodvalue>
-
- list>
-
- property>
- bean>
测试类:
- package com.ruojuan.aop.test;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.ruojuan.aop.biz.BookBiz;
-
- public class Demo1 {
-
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- //BookBiz bean =(BookBiz) context.getBean("bookBiz");
- BookBiz bean =(BookBiz) context.getBean("bookProxy");
- bean.buy("落落", "玩密室", 67.9d);
- bean.comment("落落", "还挺好玩的,刺激...");
-
-
- }
-
- }
效果:
效果出来了
后置通知(org.springframework.aop.AfterReturningAdvice):在连接点正常完成后执行的通知
配置后置通知:
MyAfterReturningAdvice.java:
- package com.ruojuan.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.AfterReturningAdvice;
-
- public class MyAfterReturningAdvice implements AfterReturningAdvice{
-
- @Override
- public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
- //目标对象的类名
- String clzName = arg3.getClass().getName();
- //当前调用的方法
- String methodName = arg1.getName();
- //当前调用方法所传递参数
- String args = Arrays.toString(arg2);
- System.out.println("【买书返利】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args+",目标对象返回值为:"+arg3);
-
-
- }
-
- }
环绕通知(org.aopalliance.intercept.MethodInterceptor):包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)
案例:修改日志系统不光要输出参数,还要输出返回值(环绕通知)
配置文件:
- package com.ruojuan.aop.advice;
-
- import java.util.Arrays;
-
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
-
- /**
- * 环绕通知=前置通知+后置通知
- *
- * @author ruojuan
- * 名称:
- * 时间:2022年8月7日 下午2:54:34
- */
- public class MyMethodInterceptor implements MethodInterceptor{
-
- @Override
- public Object invoke(MethodInvocation arg0) throws Throwable {
- //目标对象的类名
- String clzName = arg0.getThis().getClass().getName();
- //当前调用的方法
- String methodName = arg0.getMethod().getName();
- //当前调用方法所传递参数
- String args = Arrays.toString(arg0.getArguments());
- System.out.println("【环绕通知】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args);
- //方法的返回值 执行目标方法 bookBiz.buy(落落,jmp,6.6)
- Object rs = arg0.proceed();
- System.out.println("【环绕通知】:"+rs);
- return rs;
- }
-
- }
- package com.ruojuan.aop.advice;
-
- import org.springframework.aop.ThrowsAdvice;
-
- import com.ruojuan.aop.exception.PriceException;
-
- /**
- * 过于过滤通知
- * 相较于前置通知,后置通知,环绕通知有一个非常大的区别
- * 前面三大通知都需要显示其中的方法
- * 环绕通知则不需要,但是,它的方法名又是固定的;
- * @author ruojuan
- * 名称:
- * 时间:2022年8月7日 下午3:07:27
- */
- public class MyThrowsAdvice implements ThrowsAdvice{
-
- public void afterThrow(PriceException p) {
- System.out.println("[异常通知]:当价格发生异常,那么执行此处代码块!!!");
- }
-
- }
配置文件:
- <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"
- 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 class="com.ruojuan.impl.UserBizImpl1" id="userBiz">bean>
-
- <bean class="com.ruojuan.web.UserAction" id="userAction">
- <property name="userbiz" ref="userBiz">property>
- <property name="name" value="luoluo">property>
- <property name="age" value="22">property>
- <property name="hobby">
- <list>
- <value>玩鬼屋value>
- <value>吓别人value>
- <value>好开心value>
- list>
- property>
- bean>
-
- <bean class="com.ruojuan.web.OrderAction" id="orderAction">
- <property name="userbiz" ref="userBiz">property>
- <constructor-arg name="name" value="lele">constructor-arg>
- <constructor-arg name="age" value="112">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>玩密室value>
- <value>很烧脑value>
- <value>超喜欢value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.ruojuan.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.ruojuan.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="com.ruojuan.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
-
- <bean class="com.ruojuan.aop.advice.MyThrowsAdvice" id="myThrows">bean>
-
- <bean class="com.ruojuan.aop.advice.MyMethodInterceptor" id="myMethod">bean>
-
- <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterPlus">
- <property name="advice" ref="myAfter">property>
- <property name="pattern" value=".*buy">property>
- bean>
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.ruojuan.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
-
- <value>myAfterPlusvalue>
- <value>myMethodvalue>
-
- list>
-
- property>
- bean>
-
-
- beans>