转发地址:https://www.iteye.com/blog/elim-2394762

2 基于Aspectj注解的Spring Aop简单实现

Spring Aop是基于Aop框架Aspectj实现的,它不是完完全全的对Aspectj框架进行扩展和改造,而是利用Aspectj里面的一些功能来实现自己的Aop框架,其中就包括对Aspectj提供的注解的解析。之前已经提过Spring Aop和Aspectj实现的Aop之间的差别,这里就不再赘述。本文主要描述的是如何利用Aspectj提供的注解来实现Spring Aop功能,旨在让大家对Spring Aop、对使用Aspectj注解开发Spring Aop有一个初步印象。

2.1 启用对Aspectj注解的支持

使用Aspectj注解来实现Spring Aop时我们首先需要启用Spring对Aspectj注解支持的功能,这是通过配置来进行的。当我们的Spring配置是以配置文件为主时,我们可以通过在Spring的配置文件中引入aop相关的schema,然后通过<aop:aspectj-autoproxy/>来启用对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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:annotation-config/>
<context:component-scan base-package="com.elim.test"/>
<!-- 启用对Aspectj注解的支持 -->
<aop:aspectj-autoproxy/> </beans>

当我们的配置是通过使用@Configuration标注的配置类来进行的时候,我们就可以通过在该配置类上使用@EnableAspectJAutoProxy进行标注来启用对Aspectj注解的支持。

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy
public class AppConfig { }

2.2 定义切面类

定义切面类比较简单,只需要定义一个简单的使用@Aspect进行标注的类即可。@Aspect是Spring Aop识别切面类的一个标记,但是光使用@Aspect对切面类进行标注还不行。因为Spring Aop只能对定义在bean容器中的bean发生作用,对应的切面类也必须是定义在bean容器中的bean对象时其才能发现。@Aspect不具备默认让Spring扫描到它,把对应的类实例化为Spring bean的功能,所以我们必须要在bean容器中定义该bean。可以通过配置文件定义,也可以通过使用@Component进行标注等。

@Aspect
@Component
public class MyAspect { }

使用@Aspect标注的切面类也可以像普通类一样定义普通的属性和方法,如果有需要也可以把它当做一个普通的bean使用。

2.3 定义Pointcut

Pointcut是用来定义切面类需要作用的JoinPoint的,在Spring Aop中这些JoinPoint其实就是一些我们需要进行切入的方法执行,因为之前我们说过Spring Aop只支持对bean方法执行的切入。基于Aspect注解形式定义的Pointcut的核心是@Pointcut注解,我们需要在Aspect类中定义一个没有返回值的方法,方法类型可任意,然后在该方法上使用@Pointcut进行标注,表示其是一个Pointcut定义,对应的方法名即表示该Pointcut的名称。@Pointcut有一个value属性,其通常是一个表达式,通过它可以指定当前Pointcut需要作用的JoinPoint。表达式可以有很多种写法,这个在后续会专门讲解,通常用的最多的就是execution,表示方法的执行。如我们想定义一个Pointcut的JoinPoint为所有add方法的执行,那么我们可以如下定义。

@Aspect
@Component
public class MyAspect { @Pointcut("execution(* add(..))")
private void pointcut() {} }

2.4 定义Advice

Advice需要与Pointcut绑定在一起,用以定义需要在指定的Pointcut匹配的JoinPoint处执行的操作。Advice主要有三种类型,before、after和around,Aspectj对它们都有对应的注解进行支持。基于Aspectj注解的advice定义是通过对应的注解来指定的,我们需要在切面类中定义一个方法,然后在该方法上使用对应的注解进行标注。对应的advice注解都有一个value属性,我们需要通过它来指定与之绑定的Pointcut,对应的Pointcut需要通过Pointcut定义的类全名称.方法名()来指定,如果是在当前切面类中定义的Pointcut则可以省略对应的类名称。这里主要拿before来做一个示例,如下,我们在切面类中定义了一个方法before,并用@Before注解标注了该方法,同时指定了其所绑定的Pointcut为同一个切面类中定义的pointcut。

@Aspect
@Component
public class MyAspect { @Pointcut("execution(* add(..))")
private void pointcut() {} @Before("com.elim.test.spring.aop.MyAspect.pointcut()")
private void before(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget() + "----------------------Before---------------------");
} }

至此,当我们在访问Spring bean容器中任意bean对象的add方法前就会调用MyAspect切面类中定义的before方法了。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest { @Autowired
private UserService userService; @Test
public void test1() {
User user = new User(1, "ZhangSan");
userService.add(user);
} }

(注:本文是基于Spring4.1.0所写,写于2016年9月5日星期一)

Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现的更多相关文章

  1. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  2. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  3. 基于Aspectj 注解实现 spring AOP

    AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...

  4. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  5. Spring Aop(四)——基于Aspectj注解的Advice介绍

    转发地址:https://www.iteye.com/blog/elim-2395315 4 基于Aspectj注解的Advice介绍 之前介绍过,Advice一共有五种类型,分别是before.af ...

  6. 十四 Spring的AOP的基于AspectJ的注解开发

    Spring的AOP的基于AspectJ的注解开发 创建项目,引入jar包 编写目标类.切面类 配置目标类.切面类 在注解文件里开启AOP的开发 <?xml version="1.0& ...

  7. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  8. Spring同一个类中的注解方法调用AOP失效问题总结

    public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...

  9. (spring-第19回【AOP基础篇】)基于AspectJ和Schema的AOP

    基于AspectJ就是基于@AspectJ注解,基于Schema就是全部依靠配置文件.那么首先要了解Java注解. Java注解初探 在JDK5.0中,我们可以自定义标签,并通过Java语言的反射机制 ...

随机推荐

  1. centos6.5下cacti部署说明

    一.Cacti简介 1. cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户. ...

  2. 1. jenkins常见错误及解决方法

    1. Jenkins一直卡在启动页面 需要你进入jenkins的工作目录, 打开 hudson.model.UpdateCenter.xml 把 http://updates.jenkins-ci.o ...

  3. Vue入门——v-if和v-show

    v-if 特点:每次都会重新删除或创元素 有较高的切换性能消耗 v-show 特点:每次不会重新进行DOM的删除和创建操作,只是切换了元素的display:none样式 有较高的初始渲染消耗

  4. nodejs常用框架使用样例

    Koa const Koa = require('koa'); const router = require('koa-router')(); const app = new Koa(); const ...

  5. 洛谷1546 最短网络Agri-Net【最小生成树】【prim】

    [内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...

  6. Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  7. 查询Linux下文件格式.

    备忘 file 命令可以查一个文件的格式 readelf -h 可执行文件名. 可以查询可执行文件的详细的格式 向Windows中exeinfo 软件类

  8. W: GPG error: http://ppa.launchpad.net trusty InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8CF63AD3F06FC659

    报错信息: W: GPG error: http://ppa.launchpad.net trusty InRelease: The following signatures couldn't be ...

  9. JAVA RPC (九) netty服务端解析

    源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...

  10. hive 常用参数

    hive.exec.max.created.files •说明:所有hive运行的map与reduce任务可以产生的文件的和 •默认值:100000  hive.exec.dynamic.partit ...