切点 -- 切点就定义了“何处”,切点会匹配通知所要织入的一个或者多个连接点
通知 - 切面也需要完成一定的工作,在 AOP 术语中,切面的工作被称为通知
切面--切面是通知和切点的集合,通知和切点共同定义了切面的全部功能,何时何处完成其功能
链接点 -- 连接点是一个应用执行过程中能够插入一个切面的点,从应用程序的视角来看。
为什么要知道这4个点呢,因为接下来写代码的时候都是基于这四个点来写的。
切点 对应注解@pointcut ,可以自己定义需要对那些方法或这类进行拦截/切。
通知,对应用我们的@Before,@After@AfterThorwing@AfterReturn,及对方法调用的哪个时间进行通知。
切面,对应于@Aspect 用在类上面,在这里面可以定义我们的切点,通知
链接点,对应用joinpoint 对象,用来抓取我们被切方法内部的所有数据,参数,方法名等等。
使用spring AOP 的三板斧:
第一步 引入jar包,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
第二步:
在spring 配置文件,spring.xml放在resource下面,加上package scan 和 aspectj打开sop注解
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.allen.trainning.spring.aop"/>
<aop:aspectj-autoproxy/>
</beans>
第三步,
自己新建切面类,并在类上面加上注解:
@Aspect
@Component
package com.allen.trainning.spring.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogAspect {
@Before("execution(* com.allen.trainning.spring.aop.service.impl.*.*(..))")
public void before(){
System.out.println("someone get is find one xiaojieji!");
}
}
编写自己的类进行测试,即切点类:
接口:
public interface GetXiaoJieJieDao {
public XiaoJieJie getByAge(int age);
public XiaoJieJie getByName(String name);
}
实现:
@Repository
public class GetXiaoJieJieDaoImpl implements GetXiaoJieJieDao {
public XiaoJieJie getByAge(int age){
return new XiaoJieJie("Lucy",age);
}
public XiaoJieJie getByName(String name){
return new XiaoJieJie(name,18);
}
}
实体类:
public class XiaoJieJie {
public XiaoJieJie(String name, Integer age){
this.name=name;
this.age=age;
}
private String name;
private Integer age;
public void play(){
System.out.println( "my Master, I am "+name+ " ,"+age+ " years old, will you play with me");
}
}
自己的测试类:
public class TestAop {
public static void main(String[] args) {
ClassPathXmlApplicationContext xml = new ClassPathXmlApplicationContext("spring.xml");
GetXiaoJieJieDao service = xml.getBean(GetXiaoJieJieDao.class);
XiaoJieJie girl = service.getByName("ziyi");
girl.play();
}
}
执行结果:
someone get is find one xiaojieji!
my Master, I am ziyi ,18 years old, will you play with me
Process finished with exit code 0
以上代码实现了,不管谁调用了类GetXiaoJieJie类的时候,都会被抓取到,在执行该类 的方法前打印一句话。
这里只介绍了spring aop的基本思想,接下来的一篇文章会用实例介绍spring aop里面更多的更详细的知识点。