您的位置:首页 > 编程语言 > Java开发

spring基于xml配置AOP,的4种常用 切入点表达式的 步骤写法

2019-07-18 19:18 781 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq847196660/article/details/96442929

1.导入aspectjweaver是spring的切入点表达式需要用的包

<dependency>
<!--spring 包-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<dependency>
<!--aspectjweaver是spring的切入点表达式需要用的包-->
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>

2.创建业务逻辑层接口类和实现类,的增,删,改方法
3.写通知类中的具体方法 :Logger

//记录日志的工具类(也叫通知类),它里面提供了公共的代码
public class Logger {
public void beforePrintlog(){
//用于打印日志:计划让其在切入点方法执行前执行,也就是前置通知
//切入点方法就是业务层的方法
System.out.println("logger类中 前置通知 方法开始记录日志了。。。");
}
public void afterReturningPrintlog(){
//后置通知
System.out.println("logger类中 后置通知 方法开始记录日志了。。。");
}
public void afterThrowingPrintlog(){
//异常通知
System.out.println("logger类中 异常通知 方法开始记录日志了。。。");
}
public void afterPrintlog(){
//最终通知
System.out.println("logger类中 最终通知 方法开始记录日志了。。。");
}
}

4.配置通知,IOC,AOC,bean.xml 文件。

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--自己理解后的简单解释:
1.accountService 类(账户)想增强,他会的技能,并且要使用技能的时候,
都能有人监督并给出通知和建议(生成通知)。
2.就请了个可以 监督的通知类,它有通知的技能
3.通知就告诉账户,在监督账户使用技能的时候,
通知会在,账户使用具体某个技能的时候准备,  前置,后置,异常,最终
的一些相关的摄像机在进行监督。
-->

<!--配置spring的IOC ,把service 对象配置进来-->
<bean id = "accountService" class="wuwei.service.impl.AccountService"></bean>
<!--spring中 基于xml ,AOP配置步骤
1.把通知Bean也交给spring来管理
2.使用aop:config标签表明开始AOP配置
3.使用aop:aspect标签表明配置切面
id属性:是给切面提供一个唯一标识
ref属性:是指通知类bean的id
4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
我们现在示例是让pringLog方法在切入点方法执行前,所以是前置通知。
aop:before:表示配置前置通知
method属性:用于指定Logger类中哪个方法是前置通知
pointcut属性:用于指定切入点表达式,该表达式的含义是指对业务层中哪些方法增强。

切入点表达式的写法:
关键字:execution(表达式)
表达式:
访问修饰符    返回值     包名.包名..类名.方法名(参数列表)
标准示例:
pointcut="execution(public void wuwei.service.impl.
AccountService.saveAccount())"
全通配写法:
* *..*.*(..)
切入点表达式的一般写法:
一般我们都是对业务层所有实现类的所有方法进行增强,因此切入点表达式写法通常为
<aop:pointcut expression="execution
(* cn.maoritian.service.impl.*.*(..))" ></aop:pointcut>
-->
<!--配置Logger 通知类-->
<bean id = "logger" class="wuwei.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:before method="beforePrintlog" pointcut=
"execution(* wuwei.service.impl.*.*(..))"></aop:before>
<!--配置后置通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after-returning method="afterReturningPrintlog" pointcut=
"execution(* wuwei.service.impl.*.*(..))"></aop:after-returning>
<!--配置异常通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after-throwing method="afterThrowingPrintlog" pointcut=

3ff7
"execution(* wuwei.service.impl.*.*(..))"></aop:after-throwing>
<!--配置最终通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after method="afterPrintlog" pointcut=
"execution(* wuwei.service.impl.*.*(..))"></aop:after>
</aop:aspect>
</aop:config>
</beans>

5.测试类

public class AOPTest {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
IAccountService as = (IAccountService) ac.getBean("accountService");
//执行方法
as.saveAccount();
}
}

知识拓展:切入点表达式的冗长,精简

<!--配置Logger 通知类-->
<bean id = "logger" class="wuwei.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>

<!--由于 pointcut属性 中的切入点表达式 过于冗长,可以提取出来,
之后再引用就行了,但配置必须在切面之前
id 是寻找的唯一标识,expression是表达式的内容
通知表达式的关键字变为pointcut-ref = "pt1"-->
<aop:pointcut id="pt1" expression="execution(* wuwei.service.impl.*.*(..))"></aop:pointcut>

<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:before method="beforePrintlog" pointcut-ref="pt1"></aop:before>
<!--配置后置通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after-returning method="afterReturningPrintlog" pointcut-ref="pt1"></aop:after-returning>
<!--配置异常通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after-throwing method="afterThrowingPrintlog" pointcut-ref="pt1"></aop:after-throwing>
<!--配置最终通知的类型,并建立通知方法,和切入点方法(也就是业务逻辑层实现类中的方法)的关联-->
<aop:after method="afterPrintlog" pointcut-ref="pt1"></aop:after>
</aop:aspect>
</aop:config>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: