Spring基于AspectJ的AOP的开发——注解
源码:https://gitee.com/kszsa/dchart
一, AspectJ的概述:
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
Spring为了简化自身的AOP的开发,将AspectJ拿过来作为Spring自身一个AOP的开发.
二, Spring AspectJ开发实例
2.1 开发所需jar包

maven中相关包引入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aspectJ依赖包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
2.2 AspectJ 注解开发规范
2.2.1 AspectJ的切入点:
//统一管理切入点的表达式.
@Pointcut(value="execution(* com.dchart.aop.service.CustomerService+.find(..))")
private void myPointcut1(){}//这个类没有实际用途, 只是为了@Pointcut 注解
2.2.2 @AspectJ提供不同的通知类型
@Before 前置通知,相当于BeforeAdvice
在执行目标方法之前完成一个操作,获得到切入点信息.
@Before(value="execution(* com.dchart.aop.service.CustomerService+.save(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置通知============"+joinPoint);
}
@AfterReturning 后置通知,相当于AfterReturningAdvice
在目标方法执行之后完成一个操作,获得方法的返回值.
@AfterReturning(value="execution(* com.dchart.aop.service.CustomerService+.update(..))",returning="result")
public void afterReturn(Object result){
System.out.println("后置通知============"+result);
}
@Around 环绕通知,相当于MethodInterceptor
在目标方法执行的前和执行后完成一个操作,阻止目标方法执行.
@Around(value="execution(* com.dchart.aop.service.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="CustomerServiceAspect.myPointcut1()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("异常抛出通知========="+e.getMessage());
}
@After 最终final通知,不管是否异常,该通知都会执行
在目标方法任何情况下都会执行的操作.相当于finally中的代码.
@After(value="CustomerServiceAspect.myPointcut1()")
public void after(){
System.out.println("最终通知===========");
}
2.2.3 通过配置启用@AspectJ切面
1、开启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"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 开启AspectJ自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
2、对应的切面类添加注解

2.2.4 Aspect和Advisor的区别:
Advisor :传统的切面.传统切面一般都是由一个切入点和一个通知的组合.
Aspect :真正意义上的切面.由多个切入点和多个通知的组合.
2.2.5 在通知中通过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.3 Spring AspctJ 基于注解模式的开发例子
定义接口CustomerService.java
package com.dchart.aop.service;
public interface CustomerService {
public void save();
public Integer update();
public void delete();
public void find();
}
接口实现
CustomerServiceImp.java
package com.dchart.aop.service.imp;
import com.dchart.aop.service.CustomerService;
public class CustomerServiceImp 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;
}
}
编写切面类
CustomerServiceAspect.java
package com.dchart.aop.service.aspect; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class CustomerServiceAspect { //统一管理切入点的表达式.
@Pointcut(value="execution(* com.dchart.aop.service.CustomerService+.find(..))")
private void myPointcut1(){}//这个类没有实际用途, 只是为了@Pointcut 注解 @Before(value="execution(* com.dchart.aop.service.CustomerService+.save(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置通知============"+joinPoint);
} @AfterReturning(value="execution(* com.dchart.aop.service.CustomerService+.update(..))",returning="result")
public void afterReturn(Object result){
System.out.println("后置通知============"+result);
} @Around(value="execution(* com.dchart.aop.service.CustomerService+.delete(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
} @AfterThrowing(value="CustomerServiceAspect.myPointcut1()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("异常抛出通知========="+e.getMessage());
} @After(value="CustomerServiceAspect.myPointcut1()")
public void after(){
System.out.println("最终通知===========");
} }
单元测试类CustomerServiceImpTest.java
package com.dchart.aop.service.imp; import static org.junit.Assert.*; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dchart.aop.service.CustomerService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring_aop.xml")
public class CustomerServiceImpTest { @Resource(name="customerService")
private CustomerService customerService; @Test
public void test() {
customerService.save();
customerService.update();
customerService.delete();
customerService.find();
} }
Spring 配置文件:spring_aop.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"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 开启AspectJ自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 目标对象 -->
<bean id="customerService" class="com.dchart.aop.service.imp.CustomerServiceImp" />
<!-- 配置切面 -->
<bean id="myAspectAnno" class="com.dchart.aop.service.aspect.CustomerServiceAspect" /> </beans>
运行结果:

源码:https://gitee.com/kszsa/dchart
参考地址:http://www.cnblogs.com/wang-meng/p/5641687.html
Spring基于AspectJ的AOP的开发——注解的更多相关文章
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
- (转)Spring使用AspectJ进行AOP的开发:注解方式
http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...
- Spring基于AspectJ的AOP的开发之AOP的相关术语
1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...
- Spring整合AspectJ的AOP
学而时习之,不亦说乎! --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring框架学习09——基于AspectJ的AOP开发
1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...
- 基于aspectj的aop注解操作
随机推荐
- RHEL 5.7 使用rpm安装XtraBackup问题总结
在Red Hat Enterprise Linux Server release 5.7 (Tikanga)上使用RPM方式安装Percona Xtrabackup 2.4.6时遇到了一些问题,特意总 ...
- Linux系统网络文件配置
/etc 一.修改配置文档(需要重启网络配置,永远生效) 1.修改IP地址[MariaDB@db1]$ vi /etc/sysconfig/network-scripts/ifcfg-eth0DEV ...
- c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树
c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路 ...
- c/c++ 图相关的函数(二维数组法)
c/c++ 图相关的函数(二维数组法) 遍历图 插入顶点 添加顶点间的线 删除顶点 删除顶点间的线 摧毁图 取得与v顶点有连线的第一个顶点 取得与v1顶点,v1顶点之后的v2顶点的之后的有连线的第一个 ...
- oracle数据库访问形式
1. sql plus访问, sqlplus.exe 2. sql developer访问,sqldeveloper.exe 3. pl/sql 自己下载 4. browse https://loca ...
- ES5-ES6-ES7_严格模式
运行模式 正常(混杂)模式与严格模式,除了正常运行模式(混杂模式),ES5添加了第二种运行模式:"严格模式"(strict mode) 顾名思义,这种模式使得Javascript在 ...
- 设计模式のChainOfResponsibilityPattern(责任链模式)----行为模式
一.产生背景 职责链模式是一种行为模式,为解除请求的发送者和接收者之间的耦合,而使多个对象都有机会处理这个请求.将这些对象连接成一条链,并沿着这条链传递该请求,直到有一个对象处理它.避免请求发送者与接 ...
- C#のsocket通信
博主要做一个手机和电脑端(C#)通讯的程序,便览了网络上关乎socket的东西.但是接收文件的时候卡住了,怎么也接收不全.后来做了分片处理,如果分片,发送的时候就会有不同的socket(客户端开发不是 ...
- centos7下安装docker(14安装docker machine)
之前我们做的实验都是在一个host上面的,其实在真正的环境中有多个host,容器在这些host上面启动,运行,停止和销毁,相关容器会通过网络相互通信,无论他们是否运行在相同的host上面. 对于这种歌 ...
- [python] A*算法基于栅格地图的全局路径规划
# 所有节点的g值并没有初始化为无穷大 # 当两个子节点的f值一样时,程序选择最先搜索到的一个作为父节点加入closed # 对相同数值的不同对待,导致不同版本的A*算法找到等长的不同路径 # 最后c ...