目录
AOP(Aspect Oriented Programming)称为面向切面编程,利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
建一个com.cdl.aop.biz的包,里面建一个类
BookBiz
- package com.cdl.aop.biz;
-
- public interface BookBiz {
- // 购书
- public boolean buy(String userName, String bookName, Double price);
-
- // 发表书评
- public void comment(String userName, String comments);
- }
再建一个com.cdl.aop.biz.impl的包,里面建一个类
BookBizImpl
- package com.cdl.aop.biz.impl;
-
- import com.cdl.aop.biz.BookBiz;
- import com.cdl.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");
- }
- System.out.println(userName + " buy " + bookName + ", spend " + price);
- return true;
- }
-
- public void comment(String userName, String comments) {
- // 通过控制台的输出方式模拟发表书评
- System.out.println(userName + " say:" + comments);
- }
-
- }
再建一个com.cdl.aop.exception的包,里面建一个类
PriceException
- package com.cdl.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);
- }
-
- }
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志
建一个包com.cdl.aop.advice,在里面再建一个类
MyMethodBeforeAdvice
- package com.cdl.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.MethodBeforeAdvice;
- /**
- * 实现org.springframework.aop.MethodBeforeAdvice接口
- 买书、评论前加系统日志
- * @author Lenovo
- *
- */
- 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
- <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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.cdl.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.cdl.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- list>
- property>
- bean>
-
-
-
- beans>
-
新建一个com.cdl.aop.test包 建一个Demo1的类
- package com.cdl.aop.test;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.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");
- bean.buy("陈冬丽", "哈利波特", 20.7d);
- bean.comment("陈冬丽", "真好看");
-
- }
-
-
-
- }
结果:

实现买书、评论前加系统日志
- package com.cdl.aop.test;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.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("陈冬丽", "哈利波特", 20.7d);
- bean.comment("陈冬丽", "真好看");
-
- }
-
-
-
- }

实现org.springframework.aop.AfterReturningAdvice接口
买书返利
MyAfterReturningAdvice
- package com.cdl.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 returnValue, Method method, Object[] args, Object target) throws Throwable {
- //目标对象的类名
- String clzName = target.getClass().getName();
- //当前调用的方法是
- String methodName = method.getName();
- //当前调用方法所传递的参数
- String arg = Arrays.toString(args);
- System.out.println("[买书返利系统日志:]"+clzName+"."+methodName+"被调用,传递的参数为:"+args+"目标对象方法返回值为:"+returnValue);
- }
-
- }
- <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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.cdl.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- list>
- property>
- bean>
-
-
-
- beans>
Demo1不变

org.aopalliance.intercept.MethodInterceptor
类似拦截器,会包括切入点,目标类前后都会执行代码。
- package com.cdl.aop.advice;
-
- import java.util.Arrays;
-
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- /**
- * 环绕通知=前置通知+后置通知
- * @author Lenovo
- *
- */
- 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 args = Arrays.toString(invocation.getArguments());
- //方法的返回值 执行目标方法 BookBiz
- Object rs = invocation.proceed();
- System.out.println("[环绕通知:]"+clzName+"."+methodName+"被调用,传递的参数为:"+rs);
- return rs;
- }
-
- }
- <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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.cdl.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- <value>myMethodvalue>
- list>
- property>
- bean>
-
-
-
- beans>

org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
MyThrowsAdvice
- package com.cdl.aop.advice;
-
- import org.springframework.aop.ThrowsAdvice;
-
- import com.cdl.aop.exception.PriceException;
-
- /**
- * 关于异常通知
- * 相较于前置通知、后置通知、环绕通知有一个非常大的区别
- * 前面三大通知都需要实现其中的方法
- * 异常通知不需要,但是,它的方法名是固定的
- * @author Lenovo
- *
- */
- public class MyThrowsAdvice implements ThrowsAdvice{
-
- public void afterThrowing(PriceException ex) {
- 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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
-
- <bean class="com.cdl.aop.advice.MyThrowsAdvice" id="myThrows">bean>
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.cdl.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- <value>myMethodvalue>
- <value>myThrowsvalue>
- list>
- property>
- bean>
-
-
-
- beans>
将价格改成负数

org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
- <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.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
- <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
-
- <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
-
- <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
-
- <bean class="com.cdl.aop.advice.MyThrowsAdvice" id="myThrows">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.cdl.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
-
- <value>myMethodvalue>
- <value>myThrowsvalue>
- <value>myafterPlusvalue>
- list>
- property>
- bean>
-
-
-
- beans>
注意:将负数改回来
结果
