抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

快速入门

基于注解的AOP开发步骤:

  1. 创建目标接口和目标类(内部有切点)
1
2
3
4
5
6
7
8
9
10
11
package cn.imqinhao.anno;

/**
* @author qinhao
* @version 1.0
*/
public interface TargetInterface {

public void save();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.imqinhao.anno;

import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("save running...");
}
}
  1. 创建切面类(内部有增强方法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.imqinhao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
public class MyAspect {

public void before() {
System.out.println("前置增强...");
}

}
  1. 将目标类和切面类的对象创建权交给spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package cn.imqinhao.anno;

import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
@Component("target")
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("save running...");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package cn.imqinhao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
@Component("myAspect")
public class MyAspect {

public void before() {
System.out.println("前置增强...");
}

}
  1. 在切面类中使用注解配置织入关系
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.imqinhao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
@Component("myAspect")
@Aspect // 标注当前myAspect是一个切面类
public class MyAspect {

@Before("execution(* cn.imqinhao.anno.*.*(..))")
public void before() {
System.out.println("前置增强...");
}

}
  1. 在配置文件中开启组件扫描和AOP的自动代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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="cn.imqinhao.anno" />

<!--自动代理-->
<aop:aspectj-autoproxy />

</beans>
  1. 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package cn.imqinhao.test;

import cn.imqinhao.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author qinhao
* @version 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
//@ComponentScan("cn.imqinhao.anno")
public class AnnoTest {

@Autowired
private TargetInterface target;

@Test
public void test1() {
target.save();
}

}

运行结果:

1
2
前置增强…
save running…

注解通知的类型

通知的配置语法:**@通知注解(“切点表达式”)**

image-20220325184427247

切点表达式的抽取

同xml配置aop一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.imqinhao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
* @author qinhao
* @version 1.0
*/
@Component("myAspect")
@Aspect // 标注当前myAspect是一个切面类
public class MyAspect {

// ProceedingJoinPoint:正在执行的连接点===切点
// @Around("execution(* cn.imqinhao.anno.*.*(..))")
// 方式1
@Around("pointCut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前增强...");
Object proceed = proceedingJoinPoint.proceed(); // 切点方法
System.out.println("环绕后增强...");
return proceed;
}

// 方式2
@After("MyAspect.pointCut()")
public void after() {
System.out.println("最终增强...");
}

// 定义切点表达式
@Pointcut("execution(* cn.imqinhao.anno.*.*(..))")
public void pointCut(){}

}

运行结果:

1
2
3
4
环绕前增强…
save running…
环绕后增强…
最终增强…

评论