1.什么是AOP
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理 .
2.Aop在Spring中的作用
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
3.导入依赖和xml约束
<!--aop必须要导入的依赖包-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
4.第一种aop的实现方法
前置增强和后置增强都需要一个单独的类
接口
public interface servlet {
public void add();
public void delete();
public void update();
public void select();
}
实际对象
public class Impservlet implements servlet {
public void add() {
System.out.println("添加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void update() {
System.out.println("修改用户");
}
public void select() {
System.out.println("查询用户");
}
}
两个增强类
package Log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//前置增强
public class beforeLog implements MethodBeforeAdvice {
/**
*
* @param method 方法
* @param objects 参数
* @param o 对象
* @throws Throwable
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
package Log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//后置增强
public class afterLog implements AfterReturningAdvice {
/**
*
* @param returnValue 返回值
* @param method 方法
* @param objects 参数
* @param o1 对象
* @throws Throwable
*/
public void afterReturning(Object returnValue , Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了" + o1.getClass().getName()
+"的"+method.getName()+"方法,"
+"返回值:"+returnValue);
}
}
xml
<bean id="servlet" class="Impservlet"/>
<bean id="beforeLog" class="Log.beforeLog"/>
<bean id="afterLog" class="Log.afterLog"/>
<!--需要加入三行的aop 约束-->
<!--aop实现方式一-->
<aop:config>
<!--设置切入点 execution(* servlet.*(..)) 表达式代表servlet下所有的方法,任意参数-->
<aop:pointcut id="pointcut" expression="execution(* servlet.*(..))"/>
<!--切入点需要执行的类方法: 单独的类-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
测试
@Test
public void test1(){
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
servlet servlet = Context.getBean("servlet", servlet.class);
servlet.select();
}
Impservlet的select方法被执行了
查询用户
执行了Impservlet的select方法,返回值:null
5.第二种aop的实现方法
自定义的:前置增强后置增强方法在同个类中
package diy;
//自定义的切入点
public class diypoint {
public void before(){
System.out.println("-------执行方法之前------");
}
public void after(){
System.out.println("-------执行方法之后------------");
}
}
<bean id="diypoint" class="diy.diypoint"/>
<!--aop实现方式二-->
<aop:config>
<!--切入面 类-->
<aop:aspect ref="diypoint">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* servlet.*(..))"/>
<!--同一个类-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
测试
@Test
public void test2(){
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
servlet servlet = Context.getBean("servlet", servlet.class);
servlet.select();
}
-------执行方法之前------
查询用户
-------执行方法之后------------
注意:要是aop实现方法一和方法二同时在xml中存在,那么这两种方式都会执行
6.第三种aop的实现方法
注解
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class=“true”/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
<!--第三种方法aop实现-->
<bean id="annotationPoint" class="annotationPoint"/>
<!--开启注解自动装配-->
<aop:aspectj-autoproxy/>
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//设置为切面
@Aspect
public class annotationPoint {
@Before("execution(* servlet.*(..))")
public void before(){
System.out.println("====方法执行前=====");
}
@After("execution(* servlet.*(..))")
public void after(){
System.out.println("=====方法执行后====");
}
//环绕
@Around("execution(* servlet.*(..))")
public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前");
//执行目标方法
Object proceed = pj.proceed();
System.out.println("环绕后");
}
}
@Test
public void test3(){
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
servlet servlet = Context.getBean("servlet", servlet.class);
servlet.update();
}
环绕前
方法执行前
修改用户
环绕后
方法执行后