SprignAOP--动态代理
动态代理模型
代理类$Proxy类用于增强UserImpl类的work()方法
AOP术语
1、 目标类target:就是我们需要增强的那个类 如:UserImpl.class
2、 代理类proxy:就是自定义的代理的对象 如:$Proxy.class
3、 连接点joinPoint:程序执行的某个特定位置,Spring仅支持方法的连接点
如: UserImpl类中的所有方法(相当于候选人)
4、 切入点pointCut:就是在目标类中实际增强的方法
如:work()方法(获奖者)
5、 织入weave:就是将代理类中需要增强的方法放入到目标类中去执行的过程
将原方法与其他类的方法一起调用
6、 引介Introduction:引介是一种特殊的增强,它为类添加一些属性和方法(课程不使用)
7、 通知advice:将代理对象中的方法应用到目标类的过程中产生的结果。
8、 切面aspect:所有的切入点和代理对象的方法组成在一起 构成了切面
AOP准备
pom.xml添加依赖导入jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
IUser
//1 必须要有一个接口
public interface IUser {
void work();
}
UserImpl
//2 类与接口是实现关系
public class UserImpl implements IUser {
//连接点
//切点
@Override
public void work() {
System.out.println("做自己喜欢的事情");
}
//连接点
@Override
public void play() {
System.out.println("玩的时候好好玩");
}
}
Advice
public class Advice {
public void writeLog(){
System.out.println("正经人谁写日记");
}
}
UserImplTest
public class UserImplTest {
@Autowired
IUser iUser;
@Test
public void work() {
System.out.println(iUser);
iUser.work();;
}
}
AOP的xml
<!-- 配置他们的增强关系 生成一个代理类Proxy -->
o <aop:config>
<aop:pointcut id="user" expression="execution(* com.zx.service.UserImpl.*(..))"/>
<!--在调用work方法之后,调用writeLog方法-->
<!-- 切面 方法内的逻辑-->
<aop:aspect ref="advice">
<!-- try 正常运行-->
<aop:before method="before" pointcut-ref="user"></aop:before>
<aop:after-returning method="afterReturning" pointcut-ref="user"></aop:after-returning>
<!-- catch 出现错误时补救-->
<ap:after-throwing method="afterThrowing" pointcut-ref="user"></aop:after-throwing>
<!-- finally 释放资源-->
<aop:after method="after" pointcut-ref="user"></aop:after>
</aop:aspect>
<!--在Advice类中写try catch finally -->
<aop:aspect ref="advice">
<aop:around method="around" pointcut-ref="user"></aop:around>
</aop:aspect>
</aop:config>
Advice
@Component
@Aspect
public class Advice {
public void writeLog(){
System.out.println("正经人谁写日记");
}
public void before(){
System.out.println("before-------");
}
public void after(){
System.out.println("after-------");
}
public void afterReturn(){
System.out.println("afterReturn-------");
}
public void afterThrow(){
System.out.println("afterThrow-------");
}
// 环绕通知,在xml中配置
public void around(ProceedingJoinPoint point){
try {
before();
point.proceed();
afterReturning();
}catch (Throwable e){
afterThrowing();
}finally {
after();
}
}
}
AOP切面编程-切面表达式
1)什么是切面表达式
execution([修饰符] 返回值类型 包.类.方法(参数列表) );
(2)切面表达式有什么用?
符合表达式的方法,会被增强
使用* 表过任意的内容
使用… 可以表示包与子包下面的类
使用…可以写在方法(…)表示任意参数
<!-- 配置他们的增强关系 生成一个代理类-->
<aop:config >
<aop:pointcut id="work" expression="execution(* com.wzx.service..*.*(..))"/>
<!-- 我想在work方法执行 之后调用一下writeLog-->
<aop:aspect ref="advice">
<aop:after method="writeLog" pointcut-ref="work"/>
</aop:aspect>
</aop:config>
AOP的注解注入
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:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--AOP的注解配置-->
<!--包扫描创建对象 扫描包与它下面的子包中的所有类-->
<context:component-scan base-package="com.zx.service"></context:component-scan>
<!-- 开启AOP自动代理-->
<aop:aspectj-autoproxy/>
AOP切面编程-注解
几个注解
切面@Aspect+四个注解 @Before @AfterReturning @AfterThrowing@After
切面@Aspect+@Arround
Advice
@Component
@Aspect
public class Advice {
public void writeLog(){
System.out.println("正经人谁写日记");
}
@Before("execution(* com.zx.service.UserImpl.*(..))")
public void before(){
System.out.println("before");
}
@AfterReturning("execution(* com.zx.service.UserImpl.*(..))")
public void afterReturning(){
System.out.println("afterReturning");
}
@AfterThrowing("execution(* com.zx.service.UserImpl.*(..))")
public void afterThrowing(){
System.out.println("afterThrowing");
}
@After("execution(* com.zx.service.UserImpl.*(..))")
public void after(){
System.out.println("after");
}
public void around(ProceedingJoinPoint point){
try {
before();
point.proceed();
afterReturning();
}catch (Throwable e){
afterThrowing();
}finally {
after();
}
}
}