我们知道Spring有两大核心,分别是IOC和AOP,IOC控制反转已经在上篇文章给大家介绍,这篇文章我们来聊聊什么是AOP,为什么AOP会是Spring的核心?
AOP :Aspect Oriented Programming,面向切面编程
一、什么是AOP
网上的专业术语是这样说的:
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
大概就是这么回事:
通过看下面的例子应该就会明白啦
二、AOP中的一些专业术语
- 横切关注点 :跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等
- 切面(ASPECT)∶ 横切关注点被模块化的特殊对象。即使,它是一个类。·通知(Advice) :切面必须要完成的工作。即,它是类中的一个方法。
- 目标(Target)∶ 被通知对象
- 代理(Proxy): 向目标对象应用通知之后创建的对象。
- 切入点(PointCut): 切面通知执行的“地点"的定义
- 连接点(JointPoint) : 与切入点匹配的执行点。
三、Spring实现AOP 方式一:使用Spring API接口
方式一:使用Spring API接口
定义一个业务层接口:UserService
public interface UserService { public void add(); public void delete(); public void update(); public void select(); }
业务层实现类: UserServiceImpl(实现UserService)
public class UserServiceImpl implements UserService{ @Override public void add() { System.out.println("增加了一个用户"); } @Override public void delete() { System.out.println("删除了一个用户"); } @Override public void update() { System.out.println("更新了一个用户"); } @Override public void select() { System.out.println("查看了一个用户"); } }
来一个前置日志类:
//实现 MethodBeforeAdvice 接口 public class Log implements MethodBeforeAdvice { //method:要执行的目标对象的方法 //args:参数 //target:目标 @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了"); } }
来一个后置日志类:
public class AfterLog implements AfterReturningAdvice { //returnValue:返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue); } }
接着去配置一下XML:
applicationContext.xml:
<?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" 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.xsd"> <!--注册Bean--> <bean id="userService" class="zte.com.service.UserServiceImpl"></bean> <bean id="log" class="zte.com.log.Log"></bean> <bean id="afterLog" class="zte.com.afterLog.AfterLog"></bean> <!--配置AOP:需要导入aop约束 方式一:使用原生Spring API接口 --> <aop:config> <!--切入点:expression:表达式:execution():要执行的位置--> <aop:pointcut id="poincut" expression="execution(* zte.com.service.UserServiceImpl.*(..))"/> <!--执行环绕增强--> <aop:advisor advice-ref="log" pointcut-ref="poincut"></aop:advisor> <aop:advisor advice-ref="afterLog" pointcut-ref="poincut"></aop:advisor> </aop:config> </beans>
测试类:
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //注意:动态代理的是接口!!!!!!!!!!!!!!! UserService userService = (UserService) context.getBean("userService"); userService.add(); } }
运行结果为:
zte.com.service.UserServiceImpl的add被执行了 增加了一个用户 执行了add方法,返回结果为null
我们发现上面的前置日志和后置日志把输出的结果给包围了起来
四、方式二:自定义类
先写一个自定义类:
public class DiyPointCut { public void before(){ System.out.println("=====方法执行前====="); } public void after(){ System.out.println("=====方法执行后====="); } }
XML配置:
<bean id="diy" class="zte.com.diy.DiyPointCut"></bean> <aop:config> <!--自定义切面,ref要引用类--> <aop:aspect ref="diy"> <!--切入点--> <aop:pointcut id="point" expression="execution(* zte.com.service.UserServiceImpl.*(..))"/> <!--通知--> <aop:before method="before" pointcut-ref="point"></aop:before> <aop:after method="after" pointcut-ref="point"></aop:after> </aop:aspect> </aop:config>
测试类不变
输出结果:
=====方法执行前===== 增加了一个用户 =====方法执行后=====
我们发现这样也可以把自定义切入到程序内
五、方式三:注解实现
AnnotationPointCut类(名字随便都行):
@Aspect //标注这个类是一个切面 public class AnnotationPointCut { //前置增强 @Before("execution(* zte.com.service.UserServiceImpl.*(..))") public void before(){ System.out.println("=====注解方式:方法执行前====="); } //后置增强 @After("execution(* zte.com.service.UserServiceImpl.*(..))") public void after(){ System.out.println("=====注解方式:方法执行后====="); } //环绕增强:可以给定一个参数,代表我们需要获取处理切入的点 @Around("execution(* zte.com.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint pj) throws Throwable { System.out.println("环绕前"); Object proceed = pj.proceed(); System.out.println("环绕后"); } }
对XML进行简单的配置:
<!--方式三:注解方式--> <bean id="annotationPointCut" class="zte.com.diy.AnnotationPointCut"></bean> <!--开启注解支持--> <aop:aspectj-***></aop:aspectj-***>
测试类还是不变
看看运行结果:
环绕前 =====注解方式:方法执行前===== 增加了一个用户 =====注解方式:方法执行后===== 环绕后