JAVA全系列 教程
3762个小节阅读:7090.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
前置通知是在切入点之前执行的增强。
在项目中保证有Spring框架最基本环境依赖以外,还需要打入aspectj依赖
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<!-- lombok 支持 @Data等注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
新建com.bjsxt.advice.MyBefore。
MethodBeforeAdvice接口中必须重写before方法,方法中三个参数:
xxxxxxxxxx
package com.bjsxt.advice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBefore implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
}
}
xxxxxxxxxx
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.bjsxt.mapper,com.bjsxt.service.impl"></context:component-scan>
<bean id="mybefore" class="com.bjsxt.advice.MyBefore"></bean>
<aop:config>
<!-- 切入点是固定语法:execution(* 方法的全限定路径带有参数) -->
<!-- 无参方法:test()-->
<!-- 有参方法:test(int,java.lang.String)-->
<!-- 如果希望把所有叫做test的方法都匹配上,不考虑参数test(..)-->
<!-- 全限定路径的层数绝对不能少,但是如果希望对类或包做统配使用*-->
<!-- com.bjsxt.service.impl.*.*(..)-->
<aop:pointcut id="mypoint" expression="execution(* com.bjsxt.service.impl.PeopleServiceImpl.test())"/>
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"></aop:advisor>
</aop:config>
</beans>