Spring中关于AOP的实践之AspectJ方式实现通知
(本文中如有不当之处,恳请批评指正)
AspectJ方式的简化了通知的出现复杂度。但是对配置文件的操作复杂度有了一定的提升
一. 配置通知
package com.xkx.adviceDemo; import org.aspectj.lang.ProceedingJoinPoint; public class TotalAdvice { public void myBefore(){
System.out.println("aspectj方式:物资准备,人员准备,武器准备");
} public void myAfter(){
System.out.println("aspectj方式:人员解救成功");
} public void myError(){
System.out.println("解救出现意外情况,任务执行失败");
} public void myArround(ProceedingJoinPoint p){
System.out.println("环绕通知:开始解救,准备物资,准备弹药");
try {
//放行,执行切点方法
Object result=p.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕通知:任务执行成功,人质顺利解救"); } public void myBeforeHasParameter(String name1,int age1){
System.out.println("aspectj方式:"+name1+" "+age1+"物资准备,人员准备,武器准备");
} }
myBeforeHasParameter是一个带有参数的前置通知,其方法参数名要与配置文件中的对应方法完全一致
二.切点所在类的配置
package com.xkx.pojo; public class People {
private int age;
private String name;
private String sexual; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSexual() {
return sexual;
} public void setSexual(String sexual) {
this.sexual = sexual;
} @Override
public String toString() {
return this.getAge()+"--"+this.getName()+"--"+this.getSexual();
} public People() {
} public People(int age, String name, String sexual) {
this.age = age;
this.name = name;
this.sexual = sexual; }
public void crraped(){
System.out.println(this.getName()+"已被绑架");
} public void show(){
System.out.println("切点执行:解救"+this.getName()+"任务");
} public void show2(String name1,int age1){
System.out.println("切点执行:解救"+name1+"-->"+age1+"任务");
} public void covert(){
System.out.println("解救成功,召开新闻发布会");
} }
三. 配置Spring的配置文件:applicationContext.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="people2" class="com.xkx.pojo.People"></bean>
<bean id="myAdvice" class="com.xkx.adviceDemo.TotalAdvice"></bean> <aop:config>
<aop:aspect ref="myAdvice">
<aop:pointcut id="mypeople3" expression="execution(* com.xkx.pojo.People.show2(String,int )) and args(name1,age1)"></aop:pointcut>
<aop:before method="myBefore" pointcut-ref="mypeople3" ></aop:before>
<aop:before method="myBeforeHasParameter" pointcut-ref="mypeople3" arg-names="name1,age1"></aop:before>
<aop:after method="myAfter" pointcut-ref="mypeople3"></aop:after>
<aop:after-throwing method="myError" pointcut-ref="mypeople3"></aop:after-throwing>
<aop:after-returning method="myAfter" pointcut-ref="mypeople3"></aop:after-returning>
<aop:around method="myArround" pointcut-ref="mypeople3" ></aop:around>
</aop:aspect>
</aop:config>
</beans>
注意:args()中的参数名可以随意取,没有限制,但是一旦取好后,标红的通知的arg-names的参数就要与前面命名的完全一致,而通知方法的方法参数也要完全同配置文件中的通知参数名完全一致
最后的测试就不贴代码了。
Spring中关于AOP的实践之AspectJ方式实现通知的更多相关文章
- Spring中关于AOP的实践之Scheme方式实现通知
(刚开始写东西,不足之处还请批评指正) 关于AOP的通知编写方式有两种,使用Schema-baesd或者使用AspectJ方式,本篇主要介绍Schema-baesd方式的代码实现. (注:代码中没有添 ...
- Spring中关于AOP的实践之概念
一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...
- (五)Spring 中的 aop
目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数
环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
随机推荐
- ceph 常见问题百科全书---luminous安装部署篇
1. 执行步骤:ceph-deploy new node 机器:centos 7.5 ceph Luminous版本 源:阿里云 问题: Traceback (most r ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- numpy C语言源代码调试(二)
前一篇已经介绍,可以使用gdb进行调试,但是本人不太习惯gdb的文本界面,所以希望找一个比较好用的gdb的前端gui调试器. 想到的第一个是一个非常老的调试工具,DDD. DDD - Data Dis ...
- 微信小程序保存图片到相册
先来看小程序中的保存图片到相册的api wx.saveImageToPhotosAlbum({ filePath : "./test.png", //这个只是测试路径,没有效果 s ...
- [小技巧]EF Core中如何获取上下文中操作过的实体
原文地址:https://www.cnblogs.com/lwqlun/p/10576443.html 作者:Lamond Lu 源代码:https://github.com/lamondlu/EFC ...
- 轻量级原生 ajax 函数,支持 get/array post/array post/json
原生js封装 function ajaxRequest(type, url, data, callback, failCallBack, header, dataType) { var url_enc ...
- Java3y文章目录导航
由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航. 想要获取最新原创的技术文章欢迎关注我的公众号:Java3y 文章目录导航:https://github.com/Z ...
- Git操作GitHub时的Proxy配置
无论是使用Android Studio中,VCS >> Git >> Clone, 还是直接Git Clone命令,clone GitHub项目时,出现错误提示:LibreSS ...
- Linux如何管理文档多租户
例题 同一群组microsoft下的两个账号justmine001和justmine002需要共同拥有目录/microsoft/eshop的开发权,以便进行协同工作,但是其他人不允许进入和查阅该目录. ...
- CAP 2.5 版本中的新特性
前言 首先,恭喜 CAP 已经成为 eShopOnContainers 官方推荐的生产环境可用的 EventBus 之一. 自从上次 CAP 2.4 版本发布 以来,已经过去了几个月的时间,关注的朋友 ...