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 ...
随机推荐
- Linux中以单容器部署Nginx+ASP.NET Core
引言 正如前文提到的,强烈推荐在生产环境中使用反向代理服务器转发请求到Kestrel Http服务器,本文将会实践将Nginx --->ASP.NET Core 部署架构容器化的过程. Ng ...
- 大数据技术之_19_Spark学习_03_Spark SQL 应用解析 + Spark SQL 概述、解析 、数据源、实战 + 执行 Spark SQL 查询 + JDBC/ODBC 服务器
第1章 Spark SQL 概述1.1 什么是 Spark SQL1.2 RDD vs DataFrames vs DataSet1.2.1 RDD1.2.2 DataFrame1.2.3 DataS ...
- C++ : 内联函数和引用变量
一.内联函数 内联函数和普通函数的使用方法没有本质区别,我们来看一个例子,下面展示了内联函数的使用方法: #include <iostream> using namespace std; ...
- 『线段树 Segment Tree』
更新了基础部分 更新了\(lazytag\)标记的讲解 线段树 Segment Tree 今天来讲一下经典的线段树. 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间 ...
- 使用强类型实体Id来避免原始类型困扰(一)
原文地址:https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/ 作者: ...
- Smobiler Service是什么?(Smobiler——.NET移动开发平台)
在得知Smobiler即将推出新产品SmobilerService之后,许多人第一个疑问便是——Smobiler Service是什么? Smobiler的开发者对这个exe窗口一定不陌生,有时候因为 ...
- Java中三目运算符不为人知的坑
一.思考题 以下代码可能有什么错误?为什么? import java.util.HashMap; import java.util.Map; public class Test { public st ...
- Android SQLite 数据库学习
SQLite 数据库简介 SQLite 是一个轻量级数据库,它是D. Richard Hipp建立的公有领域项目,在2000年发布了第一个版本.它的设计目标是嵌入式的,而且占用资源非常低,在内存中只需 ...
- CASE 表达式
通过本篇文章我们来学习一下CASE表达式的基本使用方法. CASE表达式有简单 CASE表达式(simple case expression)和搜索 CASE表达式(searched caseexpr ...
- c# word文档的操作
参考https://blog.csdn.net/ruby97/article/details/7406806 Word对象模型 (.Net Perspective) 本文主要针对在Visual St ...