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 ...
随机推荐
- 谈谈surging引擎的tcp、http、ws协议和如何容器化部署
1.前言 分布式已经成为了当前最热门的话题,分布式框架也百花齐放,群雄逐鹿.从中心化服务治理框架,到去中心化分布式服务框架,再到分布式微服务引擎,这都是通过技术不断积累改进而形成的结果.esb,网关, ...
- 如何提高使用Java反射的效率?
前言 在我们平时的工作或者面试中,都会经常遇到“反射”这个知识点,通过“反射”我们可以动态的获取到对象的信息以及灵活的调用对象方法等,但是在使用的同时又伴随着另一种声音的出现,那就是“反射”很慢,要少 ...
- asp.net core系列 48 Identity 身份模型自定义
一.概述 ASP.NET Core Identity提供了一个框架,用于管理和存储在 ASP.NET Core 应用中的用户帐户. Identity添加到项目时单个用户帐户选择作为身份验证机制. 默认 ...
- Java8新特性之三:Stream API
Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进行非常复杂的查找.过滤.筛选等操作 ...
- Python库的安装
window下python2.python3安装包的方法 一.在线安装 安装好python.设置好环境变量后,在python安装目录下Script文件夹内会存在pip.exe和easy_install ...
- 设计模式 | 模板方法模式(template method)
定义: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 结构:(书中图,侵删) 一个定义整体框架的父类 若干不同具体实现 ...
- 使用CSS3的clip-path(裁剪路径)实现剪贴区域的显示以及实例实现图片渐变
clip-path介绍 clip-path 直译过来就是裁剪路径,使用SVG或形状定义一个HTML元素的可见区域的方法.想象一下你在Photoshop中勾勒路径的场景.MDN上是这样介绍 clip-p ...
- 关于IT公司技术委员会职责
技术委员会的核心职责 组织人才相关 • 制定职级标准 - 能力框架:知识,技能,通用素质 - 职级标准:T2T3T4-P7P8P9- • 职级评审,对齐标准 • 高端招聘,对齐标准 • 校园招聘,对齐 ...
- Git 分支模型
翻译自:https://nvie.com/posts/a-successful-git-branching-model/ 在这篇文章中,主要介绍 Git 分支模型.不会谈论任何项目的细节,只讨论分支策 ...
- Spring Boot获取前端页面参数的几种方式总结
Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...