[Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言:
在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习.
一, AspectJ的概述:
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
Spring为了简化自身的AOP的开发,将AspectJ拿过来作为Spring自身一个AOP的开发.
二, Spring AspectJ开发实例
2.1 开发所需jar包
2.2 AspectJ 注解开发规范
2.2.1 @AspectJ提供不同的通知类型
@Before 前置通知,相当于BeforeAdvice
在执行目标方法之前完成一个操作,获得到切入点信息.
@AfterReturning 后置通知,相当于AfterReturningAdvice
在目标方法执行之后完成一个操作,获得方法的返回值. @AfterReturning(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.update(..))",returning="result")
public void afterReturing(Object result){
System.out.println("后置通知============"+result);
}
@Around 环绕通知,相当于MethodInterceptor
在目标方法执行的前和执行后完成一个操作,阻止目标方法执行. @Around(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.delete(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
}
@AfterThrowing抛出通知,相当于ThrowAdvice
在目标方法出现异常的时候,完成一个操作.获得异常信息. @AfterThrowing(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("异常抛出通知========="+e.getMessage());
}
@After 最终final通知,不管是否异常,该通知都会执行
在目标方法任何情况下都会执行的操作.相当于finally中的代码. @After(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))")
public void after(){
System.out.println("最终通知===========");
}
2.2.2 通过配置启用@AspectJ切面
<?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">
<!-- 开启AspectJ自动代理-->
<aop:aspectj-autoproxy />
</beans>
2.2.3 在通知中通过value属性定义切点
通过execution函数,可以定义切点的方法切入
语法:
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
例如
匹配所有类public方法 execution(public * *(..))
匹配指定包下所有类方法 execution(* cn.itcast.dao.*(..)) 不包含子包
execution(* cn.itcast.dao..*(..)) ..*表示包、子孙包下所有类
匹配指定类所有方法 execution(* cn.itcast.service.UserService.*(..))
匹配实现特定接口所有类方法 execution(* cn.itcast.dao.GenericDAO+.*(..))
匹配所有save开头的方法 execution(* save*(..))
2.2.4 AspectJ的切入点:
统一管理切入点的表达式.
@Pointcut(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))")
private void myPointcut1(){} //这个类没有实际用途, 只是为了@Pointcut 注解
2.2.6 Aspect和Advisor的区别:
Advisor :传统的切面.传统切面一般都是由一个切入点和一个通知的组合.
Aspect :真正意义上的切面.由多个切入点和多个通知的组合.
2.3 Spring AspctJ 基于注解模式的开发
CustomerService.java:
public interface CustomerService {
public void save();
public Integer update();
public void delete();
public void find();
}
CustomerServiceImpl.java:
public class CustomerServiceImpl implements CustomerService {
@Override
public void save() {
System.out.println("保存客户...");
}
@Override
public Integer update() {
System.out.println("修改客户...");
return 100;
}
@Override
public void delete() {
System.out.println("删除客户...");
}
@Override
public void find() {
System.out.println("查询客户...");
int d = 1 / 0;
}
}
MyAspectAnno.java:
/**
* 自定义切面类:
*
*/
@Aspect
public class MyAspectAnno { @Before(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.save(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置通知============"+joinPoint);
} @AfterReturning(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.update(..))",returning="result")
public void afterReturing(Object result){
System.out.println("后置通知============"+result);
} @Around(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.delete(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
} @AfterThrowing(value="MyAspectAnno.myPointcut1()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("异常抛出通知========="+e.getMessage());
} @After(value="MyAspectAnno.myPointcut1()")
public void after(){
System.out.println("最终通知===========");
} @Pointcut(value="execution(* cn.augmentum.aspectj.demo1.CustomerService+.find(..))")
private void myPointcut1(){}
}
SpringDemo.java 测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 { @Resource(name = "customerService")
private CustomerService customerService; @Test
public void demo1() {
customerService.save();
customerService.update();
customerService.delete();
customerService.find();
}
}
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"> <!-- 使用注解完成AOP的开发 -->
<aop:aspectj-autoproxy/> <!-- 目标对象 -->
<bean id="customerService" class="cn.augmentum.aspectj.demo1.CustomerServiceImpl"/> <!-- 配置切面 -->
<bean id="myAspectAnno" class="cn.augmentum.aspectj.demo1.MyAspectAnno"/>
</beans>
2.4 Spring AspctJ 基于xml模式的开发
OrderService.java:
public class OrderService {
public void save(){
System.out.println("保存订单...");
}
public Integer update(){
System.out.println("修改订单...");
return 200;
}
public void delete(){
System.out.println("删除订单...");
}
public void find(){
System.out.println("查询订单...");
//int d = 1/ 0;
}
}
MyAspectXml.java:
public class MyAspectXml {
public void before(){
System.out.println("前置通知===========");
}
public void afterReturing(Object result){
System.out.println("后置通知==========="+result);
}
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
}
public void afterThrowing(Throwable e){
System.out.println("异常抛出通知========"+e.getMessage());
}
public void after(){
System.out.println("最终通知==========");
}
}
SpringDemo.java 测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo2 { @Resource(name="orderService")
private OrderService orderService; @Test
public void demo1(){
orderService.save();
orderService.update();
orderService.delete();
orderService.find();
}
}
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="orderService" class="cn.augmentum.aspectj.demo2.OrderService"></bean> <!-- 配置切面 -->
<bean id="myAspectXml" class="cn.augmentum.aspectj.demo2.MyAspectXml"></bean> <!-- AOP的配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.augmentum.aspectj.demo2.OrderService.save(..))" id="pointcut1"/>
<aop:pointcut expression="execution(* cn.augmentum.aspectj.demo2.OrderService.update(..))" id="pointcut2"/>
<aop:pointcut expression="execution(* cn.augmentum.aspectj.demo2.OrderService.delete(..))" id="pointcut3"/>
<aop:pointcut expression="execution(* cn.augmentum.aspectj.demo2.OrderService.find(..))" id="pointcut4"/>
<aop:aspect ref="myAspectXml">
<aop:before method="before" pointcut-ref="pointcut1"/>
<aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
<aop:around method="around" pointcut-ref="pointcut3"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
<aop:after method="after" pointcut-ref="pointcut4"/>
</aop:aspect>
</aop:config>
</beans>
OK. 到了这里Spring 基于AOP的开发也总结完了, 学习之路漫漫, 谨以此记录成长的过程!
[Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.的更多相关文章
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- spring boot集成redis基础入门
redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
- PHP基础入门(二)【PHP函数基础】
PHP基础入门(二)--函数基础 了解 PHP基础入门详解(一) 后,给大家分享一下PHP的函数基础. 这部分主要讲的就是: 函数的声明与使用.PHP中变量的作用域.静态变量.函数的参数传递.变量函数 ...
- Oracle数据库基础入门《二》Oracle内存结构
Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...
- MyBatis基础入门《二十》动态SQL(foreach)
MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...
- MyBatis基础入门《二》Select查询
MyBatis基础入门<二>Select查询 使用MySQL数据库,创建表: SET NAMES utf8mb4; ; -- ---------------------------- -- ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- Spring基础入门(二)
一.AOP 1.AOP概念 aop:面向切面编程,扩展功能不修改源代码实现. AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码. 2.AOP原理 (1)第一种情况,有接口情况,使用动态代理创建 ...
随机推荐
- Android软件设计---Dumpsys工具使用
Android中提供的dumpsys工具,用于分析Android性能.Android系统中,列出所有可用的dumpsys指令. 使用dumpsys查看memory信息: shell@aeon6735m ...
- C语言小练习三
题目要求: 定义一个二维数组保存 10个学生的5门课成绩,分别用函数实现:(1)input():输入每个学生的成绩:(2)output():输出每个学生的成绩:(3)aver_stu():计算并输出每 ...
- Java学习笔记 04 类和对象
一.类和对象的概念 类 >>具有相同属性和行为的一类实体 对象 >>实物存在的实体.通常会将对象划分为两个部分,即静态部分和动态部分.静态部分指的是不能动的部分,被称为属性,任 ...
- 1.本周的作业请参照此文:http://www.ruanyifeng.com/blog/2015/12/git-workflow.html 制定本组项目的GitHub版本更新流程---答题者:徐潇瑞
首先,介绍一下gitflow,它是最早诞生.并得到广泛采用的一种工作流程.如果采用git flow开发流程,那么项目存在两个常设分支,一个叫主分支master,另一个叫开发分支develop.mast ...
- 用Python写爬虫爬取58同城二手交易数据
爬了14W数据,存入Mongodb,用Charts库展示统计结果,这里展示一个示意 模块1 获取分类url列表 from bs4 import BeautifulSoup import request ...
- eclipse/myeclipse sublime 实时更新文件改变
情形: 在使用eclipse/myeclipse开发的时候, 像JS 或者HTML 以及一些操作时,sublime 的效率比eclipse/myeclipse要快,所以我们就可以使用这两者一起开发. ...
- [UCSD白板题] Fractional Knapsack
Problem Introduction Given a set of items and total capacity of a knapsack,find the maximal value of ...
- 关于ubuntu16.4 中安装最新的eclipse或者是STS出现页面特卡,且新建项目没有提示,preference选项中点击左侧标签右侧没反应的解决办法,参照google, 排版不太好,希望对一些小伙伴有所帮助
up vote21down votefavorite 12 Eclipse was working as good as anything on 14.04. I did a clean instal ...
- styleId妙用
styleId妙用: <html:text property="etpsBlocReg.cptlTotal" style="width: 94%;" re ...
- 【整理】--【GPIO】OK6410
S3C6410的GPIO引脚相对来说比较多,而且大部分引脚都具有多重复用功能,如何在linux上用最简单的方式来控制GPIO这需要我们好好研究一下底层的代码了,其实方法有很多种,鉴于在操作系统端控制G ...