引入AOP的依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>

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查找学生");
}
}

定义增强类

Log前置增强类

执行业务代码前先执行

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

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
* @author LeDao
* @company
* @create 2022-02-17 21:10
*/
public class BeforeLog implements MethodBeforeAdvice {

/**
* 执行业务代码前先执行
*
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
if (target != null) {
System.out.println("[BeforeLog]" + target.getClass().getName() + "下的" + method.getName() + "方法被执行了");
}
}
}

Log后置增强类

执行业务代码后再执行

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

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
* @author LeDao
* @company
* @create 2022-02-17 21:14
*/
public class AfterLog implements AfterReturningAdvice {

/**
* 执行业务代码后再执行
*
* @param returnValue 返回值
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("[AfterLog]执行了" + method.getName() + "方法,返回结果为:" + returnValue);
}
}

配置文件ApplicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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="beforeLog" class="com.ledao.log.BeforeLog"/>
<bean id="afterLog" class="com.ledao.log.AfterLog"/>

<!--配置AOP-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置!* * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.ledao.service.impl.StudentServiceImpl.*(..))"/>
<!--执行环绕增加!-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</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();
}
}

结果截图

添加学生的业务前后分别执行了BeforeLog和AfterLog