什么是AspectJ?

AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。

Aspect注解版

AspectJ自动代理

1.在xml文件中配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--目标类型-->
<bean id="service" class="cn.happy.Aspect01.UserServiceImpl"></bean><!--增强--><bean id="MyAspect" class="cn.happy.Aspect01.UserTest"></bean><!--Aspectj自动代理--><aop:aspectj-autoproxy/></beans>

  

2.创建接口、类

package cn.happy.Aspect01;/*** Created by Administrator on 2018/3/10.*/
public interface IUserService {void select();void update();
}

  

package cn.happy.Aspect01;/*** Created by Administrator on 2018/3/10.*/
public class UserServiceImpl implements IUserService {public void select() {System.out.println("select OK!");}public void update() {int i=5/0;System.out.println("update OK!");}
}

  

UserTest类

package cn.happy.Aspect01;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;/*** Created by Administrator on 2018/3/10.*/
@Aspect
public class UserTest {//前置增强/*@Before("execution(* *..Aspect01.*.select(..))")public void MyAspectBefore(){System.out.println("Before===");}*///后置增强/*@AfterReturning("execution(* *..Aspect01.*.select(..))")public void MyAspectAfter(){System.out.println("After===");}*//*//环绕增强@Around("execution(* *..Aspect01.*.select(..))")public void MyAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕增强A");pjp.proceed();System.out.println("环绕增强B");}*//*//异常增强@AfterThrowing("execution(* *..Aspect01.*.update(..))")public void MyAfterThrowing(){System.out.println("网络异常");}*///最终增强@After("execution(* *..Aspect01.*.update(..))")public void MyAfter(){System.out.println("最终增强");}//PointCut注解@Pointcut(value="execution(* *..Aspect01.*.select(..))")public void selects(){}@Pointcut(value="execution(* *..Aspect01.*.update(..))")public void update(){}@AfterReturning("selects()||update()")public void MyPointcutAfter(){System.out.println("After===");}
}

  

AspectJ  XML版

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--目标类型-->
<bean id="service" class="cn.happy.Aspect02.IUserServiceImpl"></bean><!--增强--><bean id="MyAspect" class="cn.happy.Aspect02.UserTest"></bean><aop:config><!--切点--><aop:pointcut id="MyPointCut" expression="execution(* *..Aspect02.*.select(..))"/><!--切面Aspect--><aop:aspect ref="MyAspect"><!--前置增强--><aop:before method="select" pointcut-ref="MyPointCut"/><!--后置增强--><aop:after-returning method="After" pointcut-ref="MyPointCut"/><!--环绕增强--><aop:around method="MyAround" pointcut-ref="MyPointCut"/><!--异常增强--><aop:after-throwing method="MyThrowing" pointcut-ref="MyPointCut"/><!--<!–最终增强–><aop:after method="MyAfter" pointcut-ref="MyPointCut"/>--></aop:aspect></aop:config></beans>

  

转载于:https://www.cnblogs.com/xuchangqi1/p/8550954.html

AspectJ注解版和XML版相关推荐

  1. 全国省份、城市、地区全数据(SQL版与XML版)包括各城市邮编

    这套数据共包括: 省份34个(包括港澳台地区): 城市345个(每个城市包括一个可通用的邮政编码): 城市对应的地区2862个(这个地区只的是城市中的小的区,比如:北京的海淀区). SQL版: SQL ...

  2. SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版

    SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版 注解版: 1.改druid 连接池,不改可以跳过这步 添加依赖 <dependency><groupId& ...

  3. 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式

    一.初识AOP   关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节      1.AOP:Aspect-O ...

  4. Spring Aop实例之AspectJ注解配置

    上篇博文<Spring Aop实例之xml配置>中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop. 依旧采用的jdk代理,接口和实现类代码请参考上篇博文 ...

  5. Spring AOP编程-aspectJ注解开发

    1.编写目标 public interface ICustomerService {public void save();public void search();public int update( ...

  6. 使用AspectJ注解技术实现AOP功能

    AspectJ是一个面向切面编程的框架,使用AspectJ不需要改动Spring配置文件,就可以实现Spring AOP功能.本篇结合实际案例详细讲述使用AspectJ实现AOP功能.通过本篇的学习, ...

  7. bpmn camunda版转为activiti版

    下载https://github.com/bpmn-io/bpmn-js-examples/tree/master/properties-panel模板,将app里的html文件和js文件修改为如下: ...

  8. 黑客美化版大马_2012版

    <% UserPass ="admin"'         密码 clientPassword ="a"' 生成后门一句话密码 siteurl=" ...

  9. XMind8破解版|中文破解版附带密钥(全功能版本)

    2019独角兽企业重金招聘Python工程师标准>>> xmind 本质上是由XML+ZIP的结构组成,是一种开放的文件格式,用户可以通过XMind开放的API为其开发插件或进行二次 ...

最新文章

  1. 改变示波器TDS3054D图片颜色
  2. 第十一周项目实践2 用邻接表存储的图来实现基本应用
  3. C语言字符串库函数api
  4. [Python图像处理] 十九.图像分割之基于K-Means聚类的区域分割
  5. 对一道SQL语句题目的再思考
  6. 什么是validationQuery
  7. 数据中心那些常见的问题
  8. YUV 后面数字的含义_奔富红酒“Bin”后的数字,是什么意思?
  9. loadrunner11压力测试设置
  10. mac下Flash cc2014的破解方法
  11. YEDROUDJ-NET: AN EFFICIENT CNN FOR SPATIAL STEGANALYSIS【Yedroudj-Net:一个高效的空间隐写分析CNN】
  12. Win10_此电脑_额外文件夹
  13. Excel Vba快速界面设计入门
  14. 转载内存授予(memory grants)的理解
  15. springboot中使用generator自动生成mybatis的接口、实体类和映射文件-springboot学习笔记
  16. 高级计算机网络-结课
  17. LINUX系统编程 LINUX 虚拟内存
  18. 前端 H5 横屏 独特处理方案详解
  19. Codeforces Round #631 (Div. 2) - Thanks, Denis aramis Shitov! E. Drazil Likes Heap(贪心+模拟)
  20. B 字符串处理1000MS64MB Description 输入一个英文名字。去掉该英文名字两端的空格,并首字母大写后,输出问候语Hello, 加名字 Input 一个英文名字 Output 去

热门文章

  1. 逆序枚举时常犯的一个错误
  2. 在Linux上使用logwatch分析监控日志文件
  3. java_eclipse_maven_svn_主题彩色插件_全屏插件
  4. daily scrum 11.9
  5. linux grub error 22,Linux系統grub常見錯誤問題解決
  6. (42)Xilinx FIFO IP核配置(三)(第9天)
  7. (23)触发器verilog与VHDL编码(学无止境)
  8. oracle安装检测空间china,oracle安装 - Ginn的个人空间 - OSCHINA - 中文开源技术交流社区...
  9. rtosucos和linux区别,为什么我们需要uCos?带你透彻理解RTOS
  10. http 访问mysql数据库_04.Http协议之GET请求与访问MySQL数据库