使用到的注解

@Aspect

用于标注使用这个注解的类是一个切面

@Before

执行业务代码前先执行这个方法

@After

执行业务代码后再执行这个方法

@Around

环绕增强方法使用

实现过程

Service接口

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
package com.ledao.service;

/**
* @author LeDao
* @company
* @create 2022-02-17 20:29
*/
public interface StudentService {

/**
* 添加学生
*/
void add();

/**
* 删除学生
*/
void delete();

/**
* 修改学生
*/
void update();

/**
* 根据id查找学生
*/
void findById();
}

Service接口实现类

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
package com.ledao.service.impl;

import com.ledao.service.StudentService;

/**
* @author LeDao
* @company
* @create 2022-02-17 20:30
*/
public class StudentServiceImpl implements StudentService {

@Override
public void add() {
System.out.println("添加学生");
}

@Override
public void delete() {
System.out.println("删除学生");
}

@Override
public void update() {
System.out.println("修改学生");
}

@Override
public void findById() {
System.out.println("根据id查找学生");
}
}

AnnotationPointCut增强类

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.ledao.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
* @author LeDao
* @company
* @create 2022-02-17 22:49
*/
@Aspect
public class AnnotationPointCut {

/**
* 执行方法前先执行
*/
@Before("execution(* com.ledao.service.impl.StudentServiceImpl.*(..))")
public void before() {
System.out.println("---------执行方法前---------");
}

/**
* 执行方法后再执行
*/
@After("execution(* com.ledao.service.impl.StudentServiceImpl.*(..))")
public void after() {
System.out.println("---------执行方法后---------");
}

/**
* 环绕通知
*
* @param proceedingJoinPoint 通过它调用目标方法
* @throws Throwable
*/
@Around("execution(* com.ledao.service.impl.StudentServiceImpl.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前");
//获取执行的业务代码
Signature signature = proceedingJoinPoint.getSignature();
System.out.println("执行的业务方法为:" + signature);
//执行业务代码
proceedingJoinPoint.proceed();
System.out.println("环绕后");
}
}

配置文件ApplicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--注册bean-->
<bean id="studentService" class="com.ledao.service.impl.StudentServiceImpl"/>
<bean id="annotationPointCut" class="com.ledao.diy.AnnotationPointCut"/>

<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.ledao.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author LeDao
* @company
* @create 2022-02-12 15:56
*/
public class MyTest {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService studentService = context.getBean("studentService", StudentService.class);
studentService.add();
}
}

结果截图

image-20220217230117467

PS.

如果不想使用配置文件,则需要在配置类上使用@EnableAspectJAutoProxy注解,这个注解的作用是:启动AspectJ自动代理,配置类代码如下:

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
package com.ledao.config;

import com.ledao.dao.UserDao;
import com.ledao.dao.UserDaoImpl;
import com.ledao.diy.AnnotationPointCut;
import com.ledao.service.StudentService;
import com.ledao.service.UserService;
import com.ledao.service.impl.StudentServiceImpl;
import com.ledao.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* @author LeDao
* @company
* @create 2022-02-15 20:39
*/
@Configuration
@EnableAspectJAutoProxy
public class MyConfig {

@Bean
public StudentService studentService(){
return new StudentServiceImpl();
}

@Bean
public AnnotationPointCut annotationPointCut(){
return new AnnotationPointCut();
}
}