属性:id:给切面提供一个唯一标识,ref:通知类bean的Id
- 4. 在aop:aspect标签的内部使用对应标签来配置通知的类型,aop:before表示配置前置通知
属性:method:指定通知类中哪个方法作为通知,pointcut:指定切入点表达式,用于对某些方法增强
aop:config
<aop:aspect id=“…” ref=“通知类”>
<aop:before method=“通知类中的方法” pointcut=“execution(切入点表达式)”></aop:before>
</aop:aspect>
</aop:config>
切入点表达式的格式:
-
关键字:execution(表达式)
-
表达式写法:
访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
public void com.ly.service.impl.AccountServiceImpl.saveAccount()
访问修饰符可以省略:void com.ly.service.impl.AccountServiceImpl.saveAccount()
返回值和包名可以使用通配符表示,有几级包就需要写几个*.
或者使用*..
表示当前包及其子包: * *..AccountServiceImpl.saveAccount()
类名和方法名都可以使用*
来实现通配:* *..*.*()
- 参数列表:
可以直接写数据类型:基本类型直接写名称,引用类型使用:.类名的方式,类型可以使用通配符来表示任意类型,但必须有参数。
可以使用..
表示有无参数均可,有参数可以是任意类型
- 全通配写法:
* *..*.*(..)
通知的类型: 前置通知、后置通知、异常通知、最终通知、环绕通知。
-
前置通知:在目标方法运行之前运行
-
后置通知:在我们的目标方法正常返回值后运行
-
异常通知:在我们的目标方法出现异常后运行
-
最终通知:在目标方法运行结束之后 ,不管有没有异常
-
环绕通知:是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方法
项目结构:
在pom.xml中导入相关依赖:
org.springframework
spring-context
5.0.2.RELEASE
org.aspectj
aspectjweaver
1.8.7
新建业务层接口和实现类:
/**
-
@Author: Ly
-
@Date: 2020-08-02 22:14
*/
public interface IAccountService {
/**
- 模拟保存账户
*/
void saveAccount();
/**
-
模拟修改账户
-
@param i
*/
void updateAccount(int i);
/**
-
模拟删除账户
-
@return
*/
int deleteAccount();
}
/**
-
@Author: Ly
-
@Date: 2020-08-02 22:17
*/
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println(“执行了保存”);
}
public void updateAccount(int i) {
System.out.println(“执行力修改”);
}
public int deleteAccount() {
System.out.println(“执行了删除”);
return 0;
}
}
新建通知类:
/**
-
@Author: Ly
-
@Date: 2020-08-02 22:19
*/
public class Logger {
/**
- 前置通知
*/
public void beforePrintLog(){
System.out.println(“前置Logger类中的printLog方法开始记录日志了”);
}
/**
- 后置通知
*/
public void afterReturningPrintLog(){
System.out.println(“后置Logger类中的printLog方法开始记录日志了”);
}
/**
- 异常通知
*/
public void afterThrowPrintLog(){
System.out.println(“异常Logger类中的printLog方法开始记录日志了”);
}
/**
- 最终通知
*/
public void afterPrintLog(){
System.out.println(“最终Logger类中的printLog方法开始记录日志了”);
}
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args=pjp.getArgs();//得到方法执行所需的参数
System.out.println(“前Logger类中的aroundPrintLog方法开始记录日志了”);
rtValue=pjp.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println(“后Logger类中的aroundPrintLog方法开始记录日志了”);
return rtValue;
} catch (Throwable e) {
System.out.println(“异常Logger类中的aroundPrintLog方法开始记录日志了”);
throw new RuntimeException(e);
}finally {
System.out.println(“最终Logger类中的aroundPrintLog方法开始记录日志了”);
}
}
}
线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识
一线互联网P7面试集锦+各种大厂面试集锦
学习笔记以及面试真题解析
}finally {
System.out.println(“最终Logger类中的aroundPrintLog方法开始记录日志了”);
}
}
}
线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识
[外链图片转存中…(img-0uoE1gRi-1714457930732)]
一线互联网P7面试集锦+各种大厂面试集锦
[外链图片转存中…(img-pm4MpjWx-1714457930733)]
学习笔记以及面试真题解析
[外链图片转存中…(img-EobPmQzc-1714457930733)]