AspectJ基于注解的使用

AspectJ简介

AspectJ是一个基于Java语言的AOP框架,一般

其主要用途:自定义开发

一般情况下spring自动生成代理,要配置aop,

首先确定目标类,aspectj 切入点表达式,需要导入jar包

spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE

除了以上这个关键包,还有spring所需的5个基础包,以及其他三个包,具体情况如下图:

在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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.扫描 注解类 -->
<context:component-scan base-package="com.xk.proxy.aspectJ_zhujie"></context:component-scan> </beans>

接着在service层用注解替换原先xml文件中的Bean

@Service("userServiceId")
public class UserServiceImpl implements UserService {

替换

<bean id="userServiceId" class="xxx.xxx.UserServiceImpl"></bean>

在切面类中用注解替换相应Bean

@Component
public class MyAspect {
}

替换

<bean id="myAspectId" class="xxx.xxx.xxx.aspectJ.MyAspect"></bean>

接着必须要进行aspectj 自动代理配置,否则即使声明了切面,也无法获取切面类中的方法

<!-- 1.扫描 注解类 -->
<context:component-scan base-package="xxx.xxx.xxx.aspectJ_zhujie"></context:component-scan>

添加注解@Aspect ,声明切面,以获取切面方法

@Component
@Aspect
public class MyAspect {

注解@Aspect替换了

<aop:aspect ref="myAspectId">

<aop:aspect> 将切面类 声明“切面”,从而获得通知(方法)  ref 切面类引用

接着替换 公共切入点

<aop:pointcut expression="execution(* xxx.xxx.proxy.aspectJ.UserServiceImpl.*(..))" id="myPointCut"/>

这里涉及到了切入点表达式

execution()用于描述方法

语法:execution(修饰符  返回值  包.类.方法名(参数) throws异常)

修饰符,一般省略

  返回值,不能省略,

  方法名,不能省略

  ‘ * ’表示任意

  (参数)

  ()      表示无参

(..)          表示参数任意

用注解替换为

//声明公共切入点
@Pointcut("execution(* xxx.xxx.proxy.aspectJ.UserServiceImpl.*(..))")
private void myPointCut(){
}

这里我用的是环绕通知类型,所以替换环绕

<aop:around method="myAround" pointcut-ref="myPointCut"/>
@Around(value = "myPointCut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前");
//手动执行目标方法
Object obj = joinPoint.proceed(); System.out.println("后");
return obj;
}

最后替换抛出异常

<aop:after-throwing method="myAfterThrowing" pointcut="execution(* xxx.xxx.UserServiceImpl.*(..))" throwing="e"/>
@AfterThrowing(value="execution(* xxx.xxx.UserServiceImpl.*(..))" ,throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
}

最终切面类:

/**
* 切面类,可含有多个通知
*/
@Component
@Aspect
public class MyAspect { //切入点当前有效
//@Before("execution(*xxx.xxx.UserServiceImpl.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知 : " + joinPoint.getSignature().getName());
} //声明公共切入点
@Pointcut("execution(*xxx.xxx.UserServiceImpl.*(..))")
private void myPointCut(){
} // @AfterReturning(value="myPointCut()" ,returning="ret")
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
} // @Around(value = "myPointCut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("前");
//手动执行目标方法
Object obj = joinPoint.proceed();
System.out.println("后");
return obj;
} // @AfterThrowing(value="execution(* xxx.xxx.UserServiceImpl.*(..))" ,throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
} @After("myPointCut()")
public void myAfter(JoinPoint joinPoint){
System.out.println("最终通知");
} }

最终spring配置:

<!-- 1.扫描 注解类 -->
<context:component-scan base-package="com.xk.proxy.aspectJ_zhujie"></context:component-scan> <!-- 2.确定 aop注解生效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

aop注解总结:

  @Aspect  声明切面,修饰切面类,从而获得 通知。

  通知

@Before 前置

@AfterReturning 后置

@Around 环绕

@AfterThrowing 抛出异常

@After 最终

切入点

@PointCut ,修饰方法 private void xxx(){}  之后通过“方法名”获得切入点引用

AspectJ用注解替换xml配置的更多相关文章

  1. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  2. @ComponentScan注解及其XML配置

    开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...

  3. [spring]Bean注入——使用注解代替xml配置

    使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...

  4. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  5. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

  6. 注解 和 xml 配置的优缺点【转】

    java annotation(注解) 的优点缺点 Annotation和xml各自作为配置项的优点与缺点. Annotation 一.Annotation 的优点 1.保存在 class 文件中,降 ...

  7. Spring_Task初探(注解,XML配置)

    这几天想写一个动态添加任务项目找了找Spring下的自带定时功能发现还真有,然后网上找了找资料写了个demo 写了两种方式来执行定时的任务(XML配置和注解) 先建两个普通的任务类(XML配置调用的任 ...

  8. Spring基础篇——通过Java注解和XML配置装配bean(转载)

      作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...

  9. 【SSH网上商城项目实战02】基本增删查改、Service和Action的抽取以及使用注解替换xml

    转自:https://blog.csdn.net/eson_15/article/details/51297698 上一节我们搭建好了Struts2.Hibernate和Spring的开发环境,并成功 ...

随机推荐

  1. L2-010 排座位 (并查集)

    这里唯一需要注意的是,各个输出的条件在题目中有点描述模糊. 是朋友关系,(不管是不是间接朋友关系) 既不是朋友也不是敌人(这里不用管是不是间接朋友) 是敌人关系,同时是间接朋友关系 是单纯的敌人关系, ...

  2. 【转】android SDK中的ddms使用详解

    一.查看线程信息1.展开左侧设备节点,选择进程: 2.点击更新线程信息图标: 注意:如果你没有运行或调试程序的话,这些图标是不可用的! 3.右侧选择“Threads”标签: 二.查看堆栈信息1.展开左 ...

  3. Linux systemctl命令笔记

    指令格式 systemctl [command] [unit] 常用指令 1.启动 $ systemctl start 2.停止 $ systemctl stop 3.重启 $ systemctl r ...

  4. TFT2.2

    https://cdn-learn.adafruit.com/downloads/pdf/2-2-tft-display.pdf

  5. Luogu2860 [USACO06JAN]冗余路径Redundant Paths

    Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...

  6. Matconvnet笔记(一)

    参考网址:http://www.vlfeat.org/matconvnet/ 内容参考博客:http://blog.sina.com.cn/s/blog_92cd3a1c0102x1ch.html M ...

  7. JavaScript的基本包装类型说明

    一.基本包装类型: 为了便于操作基本类型值,ECMAScript 还提供了3个特殊的引用类型:Boolean.Number和String.这些基本包装类型,具有与各自基本类型相应的特殊行为. 实际上我 ...

  8. AI R-CNN目标检测算法

    Region-CNN,简称R-CNN,是首次将深度学习应用于目标检测的算法. bounding box IOU 非极大值抑制 selective search 参考链接: https://blog.c ...

  9. SkylineGlobe系列软件对机器配置要求

    6.6版本: TerraExplorer for Desktop / Web Operating System: Windows® 7/ 8/ 10 - 64 bit recommended Proc ...

  10. Generative Adversarial Nets[LSGAN]

    0 背景 在这之前大家在训练GAN的时候,使用的loss函数都是sigmoid_cross_entropy_loss函数,然而xudon mao等人发现当使用伪造样本去更新生成器(且此时伪造样本也被判 ...