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 ...
随机推荐
- Mysql存储过程历史表备份
应用背景 SCADA采集系统需要将实时数据存入历史表.问题1:如何更简单的添加历史数据?2.海量历史数据,比如年数据,如何快速筛选 画曲线? 利用mysql的事件,每小时存一次采集数据: 每月备份历史 ...
- Electron学习笔记(一)
Electron是使用Javascript.HTML5技术构建跨平台桌面应用的技术,是目前非常活跃的一项技术,其中比较有名气的应用有微软的VS Code. 创建一个Electron应用的方式有很多,G ...
- 卷积神经网络之AlexNet
由于受到计算机性能的影响,虽然LeNet在图像分类中取得了较好的成绩,但是并没有引起很多的关注. 知道2012年,Alex等人提出的AlexNet网络在ImageNet大赛上以远超第二名的成绩夺冠,卷 ...
- solr 学习笔记1
创建核心(帮助: solr create_core -help) 例子: solr create_core -c mjj_core -d /var/solr/mjj_config (-c 是核心名称 ...
- js 异步转同步
在项目中有些逻辑或者请求依赖另一个异步请求,大家常用的方法是回调函数.现在有个高大上的解决方案:await async . async 是“异步”的简写,而 await 可以认为是 async wai ...
- Android For OpenCV的环境搭建
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效--由一系列 C 函数和少量 C++ 类 ...
- Windows API编程(SDK编程)配置VS2017——出现LNK 2019错误的win32项目如何解决
最近刚入门SDK编程,在 我终于知道为什么windowsApi学的人这么少了 这篇文章中,确实发现了这样的问题,我的教程使用VS2013->Windows桌面->win32,就诞生了能使用 ...
- CAP 2.5 版本中的新特性
前言 首先,恭喜 CAP 已经成为 eShopOnContainers 官方推荐的生产环境可用的 EventBus 之一. 自从上次 CAP 2.4 版本发布 以来,已经过去了几个月的时间,关注的朋友 ...
- 一份.NET 容器化的调查小结
小编在上个月在微信公众号"dotnet跨平台" 做了一个针对.NET 容器化的调查:https://mp.weixin.qq.com/s/oszbuIORT0G8XLLgMZzkn ...
- MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
开发背景: 最近在做一个批量数据导入到MySQL数据库的功能,从批量导入就可以知道,这样的数据在插入数据库之前是不会进行重复判断的,因此只有在全部数据导入进去以后在执行一条语句进行删除,保证数据唯一性 ...