目录
AOP 即面向切面编程
主要作用:
将核心的业务功能与非核心的业务功能进行分离。
将核心的业务功能写到目标对象中,将非核心的业务功能写到通知中
专业名词:
通知、连接点、目标对象、切入点、代理、适配器
日常开发通常应用的场景:
事物管理、日志
事物的开启:前置通知
事物的提交:后置通知
事物的回滚:异常通知
解决的问题:解决了需求的改变,造成了原有没必要改变的代码,需要去改变它;
比如:书籍的增删改,本身只需要完成增删改的功能即可,这是如果需要添加日志功能,那么需要在原有的代码基础上,去修改添加日志功能,受牵连的方法就三个(add/edit/del)了;
AOP中关键性概念
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
案例:
BookBiz
- package com.zsx.aop.biz;
-
- public interface BookBiz {
- // 购书
- public boolean buy(String userName, String bookName, Double price);
-
- // 发表书评
- public void comment(String userName, String comments);
- }
BookBizImpl
- package com.zsx.aop.biz.impl;
-
- import com.zsx.aop.biz.BookBiz;
- import com.zsx.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 买书日志
- System.out.println(userName + " buy " + bookName + ", spend " + price);
- return true;
- }
-
- public void comment(String userName, String comments) {
- // 通过控制台的输出方式模拟发表书评
- System.out.println(userName + " say:" + comments);
- }
-
- }
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">
-
-
-
-
- beans>
Demo
- package com.zsx.aop.demo;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.zsx.aop.biz.BookBiz;
-
- public class Demo {
-
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- BookBiz bean = (BookBiz) context.getBean("bookProxy");
- bean.buy("华晨宇", "三国", 12d);
- bean.comment("华晨宇", "是是是!!!!");
-
- }
- }
MyAdvice
- package com.zsx.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.MethodBeforeAdvice;
-
- /**
- * 前置日志
- * 买书、评论前加日志
- * @author Administrator
- *
- */
- public class MyAdvice 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.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
-
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- list>
- property>
- bean>
运行Demo效果图:
MyAfterAdvice
- package com.zsx.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.AfterReturningAdvice;
- import org.springframework.aop.MethodBeforeAdvice;
-
- /**
- * 前置日志
- * 买书、评论前加日志
- * @author Administrator
- *
- */
- public class MyAfterAdvice 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+";目标对象方法返回值为:"+arg0);
- }
-
- }
spring-context.xml
-
- <bean class="com.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
- <bean class="com.zsx.aop.advice.MyAfterAdvice" id="myAfter">bean>
-
-
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
-
- <property name="proxyInterfaces">
- <list>
- <value>com.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- list>
- property>
- bean>
运行Demo效果图:
spring-context.xml
-
- <bean class="com.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
- <bean class="com.zsx.aop.advice.MyAfterAdvice" id="myAfter">bean>
-
- <bean class="com.zsx.aop.advice.MyInterAdvice" id="myInter">bean>
-
-
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- <value>myIntervalue>
- list>
- property>
- bean>
MyInterAdvice
- package com.zsx.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.cglib.proxy.MethodProxy;
-
- /**
- * 环绕通知=前置+后置
- *
- * @author Administrator
- *
- */
- public class MyInterAdvice 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+";目标对象方法返回值为:"+args);
- //方法的返回值 执行目标方法
- Object rs = arg0.proceed();
- System.out.println("【环绕通知】:目标对象方法返回值为:"+rs);
-
- return rs;
- }
-
-
-
- }
运行Demo效果图:
MyThrowsAdvice
- package com.zsx.aop.advice;
-
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.springframework.aop.ThrowsAdvice;
-
- import com.zsx.aop.exception.PriceException;
-
- /**
- * 过滤通知
- * 相较于前置,后置,环绕通知有一个特大区别
- * 前面三大通知都需要实现其中的方法
- * 环绕通知则不需要,但方法名是固定的
- * @author Administrator
- *
- */
- public class MyThrowsAdvice implements ThrowsAdvice{
-
- public void afterThrowing(PriceException p) {
- System.out.println("【异常通知】:当价格发生异常,那么执行此代码");
- }
-
- }
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.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
- <bean class="com.zsx.aop.advice.MyAfterAdvice" id="myAfter">bean>
-
- <bean class="com.zsx.aop.advice.MyInterAdvice" id="myInter">bean>
-
- <bean class="com.zsx.aop.advice.MyThrowsAdvice" id="myThorw">bean>
-
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
- <value>myAftervalue>
- <value>myIntervalue>
- <value>myThorwvalue>
-
- list>
- property>
- bean>
-
-
- beans>
运行Demo效果图:
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.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
- <bean class="com.zsx.aop.advice.MyAfterAdvice" id="myAfter">bean>
-
- <bean class="com.zsx.aop.advice.MyInterAdvice" id="myInter">bean>
-
- <bean class="com.zsx.aop.advice.MyThrowsAdvice" id="myThorw">bean>
-
-
-
- <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfter2">
- <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.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
-
- <value>myIntervalue>
- <value>myThorwvalue>
- <value>myAfter2value>
- list>
- property>
- bean>
-
-
- beans>
运行Demo效果图: