springaop----springaop的使用(一)

  与大多数技术一样, AOP 已经形成了自己的术语。描述切面的常用术语有通知(advice)、切点(pointcut)和连接点(join point)。从今天开始,我们对spring的切面编程做一个总结。她们都是你漫漫长路上,只配错过的好姑娘。

 

spring中的aop

spring的切面可以应用以下的5种类型的通知:

  • 前置通知(Before):在目标方法被调用之前调用通知功能;
  • 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么;
  • 返回通知(After-returning):在目标方法成功执行之后调用通知;
  • 异常通知(After-throwing):在目标方法抛出异常后调用通知;
  • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。

对于spring中aop的支持,主要分为@AspectJ 注解驱动的切面和基于xml的spring的配置。以下我们对这两种配置做一个简单的了解。测试的代码目录如下:

以后可能对于spring的关于aop学习,这个目录可能会添加代码。application-xml.xml是学习xml配置的,application-aop.xml是学习@Aspectj注解配置的。

 

springaop的xml配置方式

 一、我们的application-xml.xml文件内容如下:

<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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="myService" class="com.linux.huhx.business.service.MyAspectService3"/><bean id="myAspect" class="com.linux.huhx.aspect.XmlUserfulAspect"/><aop:config><aop:aspect id="xmlAspect1" ref="myAspect"><aop:pointcut id="businessService" expression="execution(* com.linux.huhx.business.service.MyAspectService3.*())"/><aop:before method="beforeExecute" pointcut-ref="businessService"/><aop:after method="afterExecute" pointcut-ref="businessService"/></aop:aspect></aop:config>
</beans>

 

 二、我们的切面类XmlUserfulAspect如下:

package com.linux.huhx.aspect;/*** @Author: huhx* @Date: 2017-12-15 下午 12:31* @Desc: 切面类*/
public class XmlUserfulAspect {public void beforeExecute() {System.out.println("before execute.");}public void afterExecute() {System.out.println("after execute.");}
}

 

三、我们简单的定义一个应用切面的服务类

package com.linux.huhx.business.service;/*** @Author: huhx* @Date: 2017-12-15 下午 12:28*/
public class MyAspectService3 {public void serviceMethod() {System.out.println("my aspect service1 method.");}
}

 

四、在main的类中我们对上述的切面进行测试

package com.linux.huhx.main;import com.linux.huhx.business.service.MyAspectService3;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author: huhx* @Date: 2017-12-15 下午 12:32* @Desc: 基于xml配置的aop测试主体类*/
public class XmlServiceMain {private ApplicationContext context = null;@Beforepublic void initApplicationContext() {context = new ClassPathXmlApplicationContext("application-xml.xml");}@Testpublic void aspectServiceTest_1() {MyAspectService3 myAspectService1 = context.getBean("myService", MyAspectService3.class);myAspectService1.serviceMethod();}
}

打印的结果如下:

before execute.
my aspect service1 method.
after execute.

 

springaop中关于注解的方式

  这里面设计的代码比较多,主要是为了测试不同的知识点。这里全部贴出代码,后续再做整理。后续我们对这两种的配置方式做一个比较细致的过程讲解。使用aspectj,我们需要在pom.xml文件中添加依赖:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.11</version>
</dependency>

一、application-aop.xml的文件内容如下

<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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><aop:aspectj-autoproxy/><bean id="myService1" class="com.linux.huhx.business.service.MyAspectService1"/><bean id="myService2" class="com.linux.huhx.business.service.MyAspectService2"/><!--declare an aspect--><bean id="myAspect" class="com.linux.huhx.aspect.NotVeryUsefulAspect"/><bean id="myAspect1" class="com.linux.huhx.aspect.LittleUserfulAspect"/>
</beans>

 

二、两个测试切面的服务类如下:

  • MyAspectService1:这里面主要测试切面的5种类型的通知。
package com.linux.huhx.business.service;import java.io.FileNotFoundException;/*** @Author: huhx* @Date: 2017-12-15 上午 10:08* @Desc: 一个简单的测试aspect的实类*/
public class MyAspectService1 {public void serviceMethod() {System.out.println("myaspect service1 method.");}public String returnServiceMethod() {return "return huhx.";}public String throwExceptionMethod() {System.out.println("throw exception method.");try {throw new FileNotFoundException("not found class.");} catch (FileNotFoundException e) {e.printStackTrace();}return "hello world";}public String aroundServiceMethod1() {System.out.println("around service method.");return "huhx";}
}
  • MyAspectService2.java:这里面测试切面传递参数。
package com.linux.huhx.business.service;import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 11:17*/
public class MyAspectService2 {public void serviceMethod_1(Map<String, String> map) {System.out.println("service method." + map);}
}

 

三、两个切面的类如下

  • NotVeryUsefulAspect:是针对于上述的MyAspectService1类的,为了测试5种通知类型。
package com.linux.huhx.aspect;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;/*** @Author: huhx* @Date: 2017-12-15 上午 9:51* @Desc: 定义一个aspect*/@Aspect
public class NotVeryUsefulAspect {private static final String pointCutString = "execution(* com.linux.huhx.business.service.MyAspectService1.*())";@Before(pointCutString)public void beforeExecute() {System.out.println("before advice.");}@After(pointCutString)public void afterExecute() {System.out.println("after advice.");}@AfterReturning(value = pointCutString, returning = "retVal")public void afterReturingExecute1(String retVal) {System.out.println("after throwing " + retVal);}@AfterThrowing(value = pointCutString, throwing = "exception")public void afterThrowingExecute1(Throwable exception) {System.out.println("throwing in advice show message: " + exception.getMessage());}@Around(value = pointCutString)public void arundExecute1(ProceedingJoinPoint pjp) throws Throwable{System.out.println("before around.");System.out.println(pjp.proceed());System.out.println("after around.");}
}
  • LittleUserfulAspect:是对应于MyAspectService2类的,为了测试切面中参数的传递。
package com.linux.huhx.aspect;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 11:16* @Desc: 定义一个切面*/
@Aspect
public class LittleUserfulAspect {@Pointcut("execution(* com.linux.huhx.business.service.MyAspectService2.*(..)) && args(map,..)")public void anyMethod(Map<String, String> map) {}@Before(value = "anyMethod(map)")public void beforeExecute(Map map) {System.out.println("before execute." + map);}
}

 

四、我们的测试主体类ServiceMain

package com.linux.huhx.main;import com.linux.huhx.business.service.MyAspectService1;
import com.linux.huhx.business.service.MyAspectService2;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.HashMap;
import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 10:13* @Desc: 测试的main类*/
public class ServiceMain {private ApplicationContext context = null;@Beforepublic void initApplicationContext() {context = new ClassPathXmlApplicationContext("application-aop.xml");}@Testpublic void aspectServiceTest_1() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.serviceMethod();}@Testpublic void aspectServiceTest_2() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.returnServiceMethod();}@Testpublic void aspectServiceTest_3() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.throwExceptionMethod();}@Testpublic void aspectServiceTest_4() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.aroundServiceMethod1();}/* 其它的切面实现类 */@Testpublic void aspectServiceTest_5() {MyAspectService2 myAspectService2 = context.getBean("myService2", MyAspectService2.class);Map<String, String> dataMap = new HashMap<>();dataMap.put("username", "linux");dataMap.put("password", "12345");myAspectService2.serviceMethod_1(dataMap);}
}

 

友情链接

 

转载于:https://www.cnblogs.com/huhx/p/baseusespringaop1.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/467144.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Microsoft .NET Pet Shop 4 架构与技术分析

1&#xff0e;项目概述与架构分析微软刚推出了基于ASP.NET 2.0下的Pet Shop 4, 该版本有了一个全新的用户界面。是研究ASP.NET 2.0的好范例啊&#xff0c;大家都知道&#xff0c;一直以来&#xff0c;在.NET和Java之间争论不休&#xff0c;到底使用哪个平台开发的企业级应用性能…

nyoj 14 会场安排问题

会场安排问题 时间限制&#xff1a;3000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述学校的小礼堂每天都会有许多活动&#xff0c;有时间这些活动的计划时间会发生冲突&#xff0c;需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动&#xff0c;…

想领取开发套件,就来参加AIoT开发者大赛

你心目中的智慧校园长什么样&#xff1f; 传统校园上自习得排队等位&#xff0c;智慧校园手机一键查询空位&#xff0c;抢座占位有序便利&#xff1b; 传统校园灯光全靠手动调整&#xff0c;智慧校园灯光无感调控&#xff0c;营造全新智能光照环境&#xff1b;传统校园安全防护…

Notadd 2.0 全新 Node.js 版本~ (开发中) [从 PHP 到 node 的踩坑记]

对于 Notadd 我们本来期望它实现更多... 尽管我们也尝试做了很多努力&#xff0c;但是由于 PHP 本身的局限&#xff0c;以及考虑到开发环境配置的复杂程度&#xff0c;最终使用了折中方案。接下来&#xff0c;我们谈谈整个技术选型历程&#xff0c;也供今后相关开发者做借鉴和参…

SQL Server 数据库构架

Microsoft SQL Server™ 2000 数据存储在数据库中。在数据库中&#xff0c;数据被组织到用户可以看见的逻辑组件中。数据库还可以按物理方式&#xff0c;在磁盘上作为两个或更多的文件实现。使用数据库时使用的主要是逻辑组件&#xff0c;例如表、视图、过程和用户。文件的物理…

[教程]win10 ,ubuntu双系统安装避坑指南

这篇博客可以解决1.如何安装win10,ubuntu双系统2.如何使用win10引导Ubuntu&#xff0c;并且设置win10引导界面点击阅读原文获取更多信息。win10,ubuntu双系统的安装为什么要装双系统&#xff0c;之前用的虚拟机&#xff0c;但是虚拟机没有显卡&#xff0c;使用gazebo之类的3D仿…

nyoj 586 疯牛(二分+贪心)

疯牛 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述农夫 John 建造了一座很长的畜栏&#xff0c;它包括N (2 < N < 100,000)个隔间&#xff0c;这些小隔间依次编号为x1,...,xN (0 < xi < 1,000,000,000).但是&#xff0c;Jo…

es6--解构赋值

1&#xff1a; 基本用法 let [a,b,c] [1,2,3] 总结&#xff1a;只要等号两边都是可循环的结构&#xff0c;等号右边的就会按照相应的位置把值赋给左边 2&#xff1a;高级用法 let [x,y 1] [1,2] 1) 变量可以有默认值 let [x,y 1] [1,2] 2&#xff09;当且仅当等号右边的…

Type-C PD充电简介

一、Type-C简介自1998年以来&#xff0c;USB发布至今&#xff0c;USB已经走过20个年头有余了。在这20年间&#xff0c;USB-IF组织发布N种接口状态&#xff0c;包括A口、B口、MINI-A、MINI-B、Micro-A、Micro-B等等接口形态&#xff0c;由于各家产品不同&#xff0c;不同产品使用…

Linux字符设备驱动内幕

哈喽&#xff0c;我是老吴&#xff0c;继续记录我的学习心得。一、保持专注的几个技巧将最重要的事放在早上做。待在无干扰环境下&#xff0c;比如图书馆。意识到刚坐下开始投入工作前&#xff0c;有点负面小情绪是特别正常的现象。让“开心一刻”成为计划的一部分。拥有合情合…

线性表的顺序存储

线性表是n个类型相同数据元素的有限序列&#xff0c;对n>0,除第一个元素无直接前驱&#xff0c;最后一个元素没有直接后继外&#xff0c;其他都是每个元素有一个直接前驱和直接后继&#xff0c;而且是一对一的关系。 线性表&#xff1a;顺序存储&#xff0c;链式存储。 &…

mysql 5.6.38 数据库编译安装

一、系统环境&#xff1a; # cat /etc/redhat-release CentOS release 6.9 (Final)二、mysql 编译安装&#xff1a; 1、安装依赖包&#xff1a; yum install -y ncurses-devel libaio-devel cmake2、创建mysql管理用户&#xff1a; useradd -s /sbin/nologin -M mysql id m…

旧地重游

光阴飞逝1991年跟随父母搬迁至长沙&#xff0c;至今已有16年。2007年春节舅妈大寿借此机会回到儿时旧地以了多年来的心愿。经过1个小时左右颠簸终于快回到儿时生长的地方&#xff0c;那里的路面年久失修&#xff0c;经过昨天的大雨&#xff0c;已经坑坑洼洼路面到处积水&#x…

我要不要离职?

1#读者提问来到四线城市的小公司&#xff0c;其他员工都是公司主动找到他们转正的&#xff0c;有的一个月有的三个月&#xff0c;而我半年过去了&#xff0c;以为时间到了也跟他们一样自动帮我转正。然而没有&#xff0c;昨天忍不住问了公司&#xff0c;今天就拿转正表来给我填…

单链表的应用 就地逆置

【问题描述】试实现线性表的就地逆置算法&#xff0c;即在原表的存储空间将线性表&#xff08;a1,a2,a3....an&#xff09;逆置为&#xff08;an...a3,a2,a1&#xff09;. [分析]就地逆置就是不需要额外申请结点空间&#xff0c;只需要利用原来的表中的结点空间。若对顺序表中的…

关于arguments的用法

当函数的参数个数无法确定的时候&#xff1a;用 arguments。函数的 arguments 对象并不是一个数组&#xff0c;它相当于是一个实参的集合&#xff0c;但是访问单个参数的方式与访问数组元素的方式相同。访问 函数中的第n个参数 则可以使用arguments[n-1]。 1、arguments实际为实…

搞懂进程组、会话、控制终端关系,才能明白守护进程干嘛的?

守护进程概念&#xff1a;守护进程&#xff0c;也就是通常所说的Daemon进程&#xff0c;是Linux中的后台服务进程。周期性的执行某种任务或等待处理某些发生的事件。Linux系统有很多守护进程&#xff0c;大多数服务都是用守护进程实现的。比如&#xff1a;像我们的tftp&#xf…

关于编译C#文件

使用csc.exe编译非控制台应用程序,常使用/target选项此选项可简写为/t,用来指定要创建的文件类型. 如编译一个类库文件(dll)Class1.cs: namespaceTest...{ /**//// <summary> /// Class1 的摘要说明。 /// </summary> public class Class1 ...{ …

中缀表达式转换为前缀及后缀表达式并求值【摘】

它们都是对表达式的记法&#xff0c;因此也被称为前缀记法、中缀记法和后缀记法。它们之间的区别在于运算符相对与操作数的位置不同&#xff1a;前缀表达式的运算符位于与其相关的操作数之前&#xff1b;中缀和后缀同理。举例&#xff1a;(3 4) 5 - 6 就是中缀表达式- 3 4 …

H5-geolocation学习

geolocation——定位PC——IP地址精度比较低IP库Chrome -> Google手机——GPSwindow.navigator.geolocation单次 getCurrentPosition(成功, 失败, 参数)enableHighAccuracy 高精度模式——更慢、更费电timeout 超时maximumAge 缓存时间…