spring+aspectJ的实现
AspectJ:(Java社区里最完整最流行的AOP框架)
spring自身也有一套AOP框架,但相比较于AspectJ,更推荐AspectJ
在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。
AspectJ最强大的地方在于他的切入点表达式:
语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常)
修饰符,一般省略
public 公共方法
* 任意
返回值,不能省略
void 返回没有值
String 返回值字符串
* 任意
包
com.zby.service 固定包
com.zby.oa.*.service oa包下面子包 (例如:com.zby.oa.flow.service)
com.zby.oa.. oa包下面的所有子包(含自己)
com.zby.oa.*.service.. oa包下面任意子包,固定目录service,service目录任意包
类
UserServiceImpl 指定类
*Impl 以Impl结尾
User* 以User开头
* 任意
方法名,不能省略
addUser 固定方法
add* 以add开头
*Do 以Do结尾
* 任意
(参数)
() 无参
(int) 一个整型
(int ,int) 两个
(..) 参数任意
throws ,可省略,一般不写
AspectJ支持5种类型的通知注解:
before:前置通知(应用:各种校验)
在方法执行前执行,如果通知抛出异常,阻止方法运行
afterReturning:后置通知(应用:常规数据处理)
方法正常返回后执行,如果方法中抛出异常,通知无法执行,必须在方法执行后才执行,所以可以获得方法的返回值。
around:环绕通知(应用:十分强大,可以做任何事情)
方法执行前后分别执行,可以阻止方法的执行,必须手动执行目标方法
afterThrowing:抛出异常通知(应用:包装异常信息)
方法抛出异常后执行,如果方法没有抛出异常,无法执行
after:最终通知(应用:清理现场)
方法执行完毕后执行,无论方法中是否出现异常
当然,最重要也最常用的还是环绕通知,因为环绕通知必须手动执行目标方法,所以,可以代替其他几个通知
项目结构图:
一:maven项目依赖
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>demo</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>demo</name>
- <description>demo</description>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>4.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>4.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>4.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.8.10</version>
- </dependency>
- </dependencies>
- </project>
二:声明切点
I.切面首先是一个IOC中的bean,即加入@Component注释
- package com.debo.aspect.service;
- import org.springframework.stereotype.Component;
- @Component
- public class AspectService {
- //添加
- public void add() {
- }
- //修改
- public void update() {
- }
- //删除
- public void delete() {
- }
- }
三:声明切面
I.切面首先是一个IOC中的bean,即加入@Component注释
II.切面还需要加入@Aspect
- package com.debo.aspect.service;
- import java.lang.reflect.Method;
- 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;
- import org.springframework.stereotype.Component;
- @Aspect
- @Component
- public class MyAspectJ {
- // 多个方法需要使用这个切入点表达式,定义为一个公用的
- @Pointcut("execution(* com.debo.*.service.*.add*(..))")
- public void addCell() {
- }
- @Before("execution(* com.debo.*.service.*.update*(..))")
- public void updateCell() {
- System.out.println("更新的前置通知");
- }
- @After("execution(* com.debo.*.service.*.delete*(..))")
- public void deleteCell() {
- System.out.println("删除的最终通知");
- }
- @AfterReturning(value = "addCell()", returning = "ret")
- public void returnAdd(JoinPoint joinPoint, Object ret) {
- System.out.println("添加的后置通知 : " + joinPoint.getSignature().getName());
- }
- @Around("addCell()")
- public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
- System.out.println("环绕通知执行方法前");
- // 手动执行目标方法
- Object obj = joinPoint.proceed();
- System.out.println("环绕通知执行方法后");
- return obj;
- }
- @AfterThrowing(value = "addCell()", throwing = "e")
- public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {
- System.out.println("抛出异常通知 : " + e.getMessage());
- }
- }
四:xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"<span style="color:#ff0000;"> xmlns:aop="http://www.springframework.org/schema/aop"</span>
- <span style="color:#ff0000;">xmlns:context="http://www.springframework.org/schema/context"</span>
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- 注解扫描 -->
- <context:component-scan base-package="com.debo.*.service" />
- <!-- 使AspectJ注解起作用:自动为匹配的类生成代理对象 -->
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans>
五:测试
- package com.debo.aspect.service;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- public static void main(String[] args) {
- // 1.创建Spring的IOC容器
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- // 2.从IOC容器中获取bean的实例
- AspectService aspectService = ctx.getBean(AspectService.class);
- aspectService.add();
- aspectService.update();
- aspectService.delete();
- }
- }
原文链接:http://blog.csdn.net/qq_37936542/article/details/79555762
spring+aspectJ的实现的更多相关文章
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- Spring AspectJ AOP 完整示例
http://outofmemory.cn/java/spring/AOP/aop-aspectj-example-before-after-AfterReturning-afterThrowing- ...
- Spring(十二)--Spring AspectJ
Spring AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. As ...
- Spring AspectJ基于注解的AOP实现
对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发.所以,S ...
- Spring @AspectJ 实现AOP 入门例子(转)
AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...
- spring AspectJ的Execution表达式
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
- spring AspectJ的Execution表达式说明
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execut ...
- Spring AspectJ的Execution表达式-备忘笔记
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
- Spring AspectJ切入点语法详解
1.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指 ...
- spring AspectJ切入点语法详解 记录以便查阅
AspectJ切入点语法详解 6.5.1 Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spri ...
随机推荐
- ListCtrl添加右键菜单(ListCtrl类里编辑,给ListCtrl 发送NM_RCLICK消息)
在开发中会用到右键菜单,我们来一起学习一下. 假如,我们现在已经准备好了列表,就差右键处理了. 1.在资源视图中的添加一个MENU,如图 2.给要添加右键菜单的ListCtrl子类,添加消息 按 ct ...
- 【Tomcat】严重: Context [/grouponAdminWeb] startup failed due to previous errors
1 tomcat 6600启动报错[root@localhost webapps]# sh /usr/local/apache-tomcat-6.0.37_6600/bin/startup.s ...
- Behavioral模式之Visitor模式
1.意图 表示一个作用于某对象结构中的各元素的操作.它使你能够在不改变各元素的类的前提下定义作用于这些元素的新操作. 2.别名 无 3.动机 考虑一个编译器.他将源程序表示为一个抽象语法树.该编译器须 ...
- 为什么选择Solr?
在大型的SQL数据库上很难执行高速的查询有Solr是Apache 下的一个开源项目,使用Java基于Lucene开发的全文检索服务: 它是一个独立的企业级搜索应用服务器,它对外提供类似于Web-ser ...
- Django环境搭建(一)
搭建Django环境之前先搭建python运行环境 需要了解: 解释器(编译器): 计算机不能直接理解任何除机器语言外的其他语言,所以程序员必须要把自己写的语言翻译成机器语言,而将其他语言翻译成机器语 ...
- 1.1 Python基础知识 - 变量
1.什么是变量? 变量是可以通过变量名访问的内存地址,变量通常是可变的. 2.怎样去定义? 变量格式: 变量名 = "变量值" 例如: name = "Zhanghk&q ...
- JS实现跑马灯效果(鼠标滑入可暂停,离开继续跑)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- (转)Oracle RAC日常管理命令
转自:http://www.xuebuyuan.com/1206937.html 一.查看RAC环境 RAC架构,2节点信息 节点1 SQL> show parameter instance N ...
- autohotkey word getfullname (ComObjActive)
直接使用ComObjActive
- CImage将图片转为指定像素大小
CFileDialog fDlg(true, "jpg", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, &q ...