Aspectj实现AOP有两种方式:

(1)基于aspectj的xml配置;

(2)基于aspectj的注解方式;

一、基于aspectj的xml配置:

1、导入相关的AOP的jar包:

2、创建Spring核心配置文件,导入aop的约束

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

3、使用表达式配置切入点:

(1)表达式格式:

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

(2)常用的表达式举例:

举例:

①execution(* com.zwp.aop.User.add(..))

②execution(* com.zwp.aop.User.*(..))

③execution(* *.*(..))

④匹配所有save开头的方法:execution(* save*(..))

第一个*:表示所有的修饰类型。

(3)代码测试:

//原始方法:
public class MainTest {public void text1(){System.out.println("主方法....");}
}
//增强的方法:
public class SecondText {public void before1(){System.out.println("前置增强");}public void after1(){System.out.println("后置增强");}//环绕增强public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{//方法之前:System.out.println("方法之前...");//执行方法:proceedingJoinPoint.proceed();//方法之后:System.out.println("方法之后...");}
}

spring的applicationContext.xml配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 使用aop操作 start--><!-- 1、创建两个类的对象 -->    <bean id="mainTest" class="com.zwp.aop.MainTest"></bean><bean id="secondText" class="com.zwp.aop.SecondText"></bean><!-- 2、配置aop操作 --><aop:config><!-- 2.1配置切点 --><aop:pointcut expression="execution(* com.zwp.aop.MainTest.*(..))" id="pointcut1"/><!-- 2.2配置切面:即把增强用到方法上面的过程 --><aop:aspect ref="secondText"><aop:before method="before1" pointcut-ref="pointcut1"/><aop:after method="after1" pointcut-ref="pointcut1"/><aop:around method="round1" pointcut-ref="pointcut1"/></aop:aspect></aop:config><!-- 使用aop操作 end-->
</beans>

测试类:

public class Test2 {@Testpublic void test4(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");MainTest mainTest = (MainTest) context.getBean("mainTest");mainTest.text1();}
}

运行结果:

二、基于aspectj的注解方式:

(1)导入与AOP相关的jar包:

(2)创建对象:

(3)开启Aop操作:

(4)在增强类使用注解@Aspect,并在方法上使用注解完成增强配置。

测试代码:

//目标对象Target
public class Annoaop {public void text1(){System.out.println("基于aspecj的注解aop操作的主方法....");}
}

在spring的applicationContext.xml文件配置中,创建对象与开启AOP操作:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 基于aspecj的注解aop操作 start --><!-- 1.开启aop操作 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 2.创建对象 --><bean id="anno" class="com.zwp.annoAop.Annoaop"></bean><bean id="add" class="com.zwp.annoAop.Add"></bean><!-- 基于aspecj的注解aop操作 end -->
</beans>
//类说明:增强类
//3.1在增强类上面使用注解
@Aspect
public class Add {//3.2 在方法上使用注解完成增强配置:@Before(value="execution(* com.zwp.annoAop.Annoaop.*(..))")public void before1(){System.out.println("前置增强...");}
}
public class Test2 {@Testpublic void test5(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");Annoaop annoaop = (Annoaop) context.getBean("anno");annoaop.text1();}
}

运行结果:

Spirng使用Aspectj实现AOP相关推荐

  1. Spring基于AspectJ实现AOP操作

    基于AspectJ实现AOP操作 准备工作 在项目工程里面引入 AOP 相关依赖. 如果是maven项目,使用pom.xml代替引入jar包的过程(注意) 学会使用切入点表达式 AOP 操作(Aspe ...

  2. Spring使用AspectJ开发AOP

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  3. 使用AspectJ开发AOP更加便捷,你不知道嘛

    前文 中,已经讲解了Spring传统的AOP开发,但在实际开发中,我们都是使用AspectJ进行AOP开发. AspectJ 简介 AspectJ 是一个基于Java语言的独立的AOP框架. 在没有A ...

  4. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  5. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): Java代码   package com.samt ...

  6. AspectJ对AOP的实现

    引言 1.对于AOP编程思想,很多框架都进行了实现,Spring就是其中之一,可以完成面向切面编程.然后,AspectJ也实现了AOP的功能.并且实现的更加简单.方便,并且还支持注解式开发.所以,sp ...

  7. Spring框架基于AspectJ的AOP开发规范和步骤

    AOP和动态代理的关系: AOP术语: 横向关注点: 需要新增的到业务代码中的功能(在目标对象那里叫横切关注点,在切面类中叫通知) 切面类: 封装了增强方法(横向关注点)的类 通知: 切面类中的每一个 ...

  8. Spring AOP编程-传统基于aspectJ切点AOP开发

    1.在配置文件上方增加aop相关配置. 2.在spring的配置文件中定义目标与通知. 3.使用aop:xxx标签来完成切面与切点声明. 4.我们使用aspectj的切面声明方式 需要在导入aspec ...

  9. Spring中基于注解@AspectJ的AOP实现

    @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明aspects 的一种风格.通过在基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的. a ...

最新文章

  1. 引发热议的AI翻译歧视,是理所应当的事么?
  2. 迷你DVD管理器项目
  3. php调用hive,如何进行hive的简单操作
  4. 1080 MOOC期终成绩 (25 分)
  5. oracle主从表分离怎么实时更新数据_高可用数据库UDB主从复制延时的解决
  6. python 查看当前目录_Python学习第156课--ls的运用、环境变量以及PATH
  7. GIT关联本地仓库与远端仓库
  8. zabbix-自定义监控项
  9. 你真的会玩SQL吗?透视转换的艺术
  10. ajaxSubmit 提交form 表单
  11. jq css3实现跑马灯+大转盘
  12. 庆祝北大“如何制作MOOC”课程取得优秀成绩
  13. 女人一生必须拥有的珍珠
  14. 华为云服务器更换操作系统,华为云如何更换操作系统
  15. Android人脸检测方案汇总
  16. IO赛制神器——对拍
  17. Windows驱动_WSK驱动之三WSK编程注意事项
  18. SolidWorks安装出现该产品组件的 Windows Installer 没按预期运行
  19. unable to negotiate with xxxxx port xxx: no matching host key type found. Their offer: ssh-rsa
  20. 【Matlab 六自由度机器人】基于蒙特卡罗方法(Monte Carlo Method)构建机器人工作空间(附MATLAB建模仿真完整代码)

热门文章

  1. 商城客户细分数据(kaggle)
  2. 【Python金融量化 1- 100 】三、流行股票指数
  3. 代码+实例:深度学习中的“轴”全解
  4. 基于知识图谱的问答系统入门—NLPCC2016KBQA数据集
  5. HDU2176 【 Nim博弈】 SG函数求解
  6. 02 | 服务治理:Nacos 如何实现微服务服务治理
  7. 【JVM调优工具篇】使用MAT工具分析dump文件(查看GC Roots)
  8. 汇编语言——《分支与循环程序设计》实验任务书
  9. 信任与信誉对电子服务的重要性
  10. java函数ao活动对象_JavaScript中的执行上下文和变量对象