拦截器会先拦截一些指定的方法,并且对其做一些逻辑操作,这样的好处就是不改变别人的代码而对别人的代码进行操作
切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
通知(Advice): 切面必须要完成的工作
目标(Target): 被通知的对象
代理(Proxy): 向目标对象应用通知之后创建的对象
连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。
切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。
首先在你的resources包下建立一个类在建一个xml文件,这里就上图了:
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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.ps.journal"></context:component-scan>
<!-- expression:切点表达式 -->
<!--
* 用于通配包名 类名 返回值
*..* 代表通配多个*
.. 代表任意多个参数
-->
<aop:config>
<aop:pointcut expression="execution(* com.ps.*.EmpMethod.*(..))" id="aop"/>
<aop:aspect ref="inform">
<aop:before method="prepose" pointcut-ref="aop"/> <!-- 前置 -->
<aop:after method="postposition" pointcut-ref="aop"/> <!-- 后置 -->
<aop:after-throwing method="unusual" pointcut-ref="aop"/> <!-- 异常 -->
<aop:around method="embrace" pointcut-ref="aop"/> <!-- 环绕 -->
</aop:aspect>
</aop:config>
</beans>
然后做一个测试类:
package com.ps.journal;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
@Component(value="empo")
public class EmpMethod {
public void addEmp() {
try {
FileOutputStream file = new FileOutputStream("d:/a.txt");
file.write("张三".getBytes());
file.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String queryEmp() {
try {
BufferedReader br = new BufferedReader(new FileReader("d:/a.txt "));
String readLine = br.readLine();
br.close();
return readLine;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 异常
* @throws IOException
*/
public void unusual() throws IOException {
System.out.println("本身出现异常");
// 抛出一个IO异常
throw new IOException();
}
}
在做一个通知类:
package com.ps.journal;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* 通知的顺序:没有异常
* 前置通知
* 环绕之前
* 业务方法
* 环绕之后
* 后置通知
*
* 有异常
* 前置通知
* 环绕之前
* 业务方法
* 异常通知
* 后置通知
* @author
*
* 2018年11月1日下午7:59:26
*/
@Component(value="inform")
public class Inform {
/**
* 前缀通知
*/
public void prepose(JoinPoint jp) {
System.out.println("前缀通知"+jp.getSignature().getName());
}
/**
* 后缀通知
*/
public void postposition(JoinPoint jp) {
System.out.println("后缀通知"+jp.getSignature().getName());
}
/**
* 异常通知
*/
public void unusual(JoinPoint jp) {
System.out.println("异常通知"+jp.getSignature().getName());
}
/**
* 环绕通知
* @throws Throwable
*/
public void embrace(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕之前");
pj.proceed(pj.getArgs()); //手工调用业务方法
System.out.println("环绕之后");
}
}
接下来一个main方法查看效果:
package com.ps.journal;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws IOException{
ClassPathXmlApplicationContext cpc = new ClassPathXmlApplicationContext("journal/journal.xml");
EmpMethod emps = (EmpMethod) cpc.getBean("empo");
emps.unusual();
}
}
注意JoinPoint类会报一个不存在的错误,这里可以导一个架包:
https://mvnrepository.com/artifact/org.aspectj/aspectjweaver/1.9.2