开发者社区> 青夜之衫> 正文

【Spring实战】—— 13 AspectJ注解切面

简介:
+关注继续查看

前面了解了典型的AOP基于配置的使用方法,下面介绍下如何依赖于注解来实现AOP。

基于注解降低了配置文件的复杂程度,但是引入了程序间的耦合,其中的优劣待用户自己判断了。

需要注意的是,确定AspectJ与JDK之间的版本,否则会报错,详情请见

  首先看一下基于注解的切面类,这时的切面不仅仅是一个POJO类了,与AOP进行了紧密的耦合。但是配置过程和方式都与原来的方式差不多。

复制代码
package com.spring.test.chap44;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Audience {
    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(..))")
    public void performance(){}
    
    @Before("performance()")
    public void takeSeats(){
        System.out.println("takeSeats()");
    }
    @Before("performance()")
    public void turnOffCellphones(){
        System.out.println("turnOffCellphones()");
    }
    @AfterReturning("performance()")
    public void applaud(){
        System.out.println("applaud()");
    }
    @AfterThrowing("performance()")
    public void demandRefund(){
        System.out.println("demandRefund()");
    }
}
复制代码

  接下来是其他一些必不可少的类:

  切点接口类:

package com.spring.test.chap44;

public interface Performer {
    public void perform();
}

  切点实现类:

复制代码
package com.spring.test.chap44;

import org.springframework.stereotype.Component;

@Component
public class Instrumentalist implements Performer{
    public void perform() {
        System.out.println("__________ perform ___________");
    }
}
复制代码

  测试类:

复制代码
package com.spring.test.chap44;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        
        Performer performer = (Performer)ctx.getBean("xingoo");
        performer.perform();
    }
}
复制代码

  下面是重点的配置文件

  此时的配置文件注意要使spring知道哪一个是普通的bean,哪一个是通知。因此需要加上一个属性,保证AOP自动的识别通知。

<aop:aspectj-autoproxy proxy-target-class="true"/>

  配置文件如下:

复制代码
<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="xingoo" class="com.spring.test.chap44.Instrumentalist"/>
    <bean id="audience" class="com.spring.test.chap44.Audience" />
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
复制代码

  执行结果如下:

turnOffCellphones()
takeSeats()
__________ perform ___________
applaud()

 

  如果需要使用around只需要在切面中添加如下的代码就可以了:

复制代码
    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinpoint){
        try{
            System.out.println("11111");
            
            long start = System.currentTimeMillis();
            
            joinpoint.proceed();
            
            long end = System.currentTimeMillis();
            
            System.out.println("time—— "+(end-start)+" millinseconds");
            System.out.println("22222");
        }catch(Throwable t){
            System.out.println("in watchPerformance Throwable()");
        }
    }
复制代码

   对于参数的传递的通知,也与原先通过配置的差不多

  在切面中配置好切点的方法,注意带上参数

复制代码
    private String str;
    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(String)) && args(str)")
    public void performance(String str){}
    
    @Before("performance(str)")
    public void takeSeats(String str){
        System.out.println("takeSeats()"+str);
    }
复制代码

  其他的基本都不用动了,只要把切点的方法,修改成带有参数的就可以了

public class Instrumentalist implements Performer{
    public void perform(String str) {
        System.out.println("__________ perform ___________" + str);
    }
}

 

本文转自博客园xingoo的博客,原文链接:【Spring实战】—— 13 AspectJ注解切面,如需转载请自行联系原博主。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Spring Boot 项目代码混淆,实战来了,再也不用担心代码泄露了!
Spring Boot 项目代码混淆,实战来了,再也不用担心代码泄露了!
8 0
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache抽象详解的核心原理探索
缓存的工作机制是先从缓存中读取数据,如果没有再从慢速设备上读取实际数据,并将数据存入缓存中。通常情况下,我们会将那些经常读取且不经常修改的数据或昂贵(CPU/IO)的且对于相同请求有相同计算结果的数据存储到缓存中。
11 0
Spring Cloud【Finchley】实战-07异步下单
Spring Cloud【Finchley】实战-07异步下单
9 0
Spring Cloud【Finchley】实战-05配置中心的搭建(配合使用Eureka)和Config Server高可用
Spring Cloud【Finchley】实战-05配置中心的搭建(配合使用Eureka)和Config Server高可用
16 0
Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块
Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块
12 0
Spring Cloud【Finchley】实战-03订单微服务与商品微服务之间的调用
Spring Cloud【Finchley】实战-03订单微服务与商品微服务之间的调用
15 0
Spring Cloud【Finchley】实战-02订单微服务(下)
Spring Cloud【Finchley】实战-02订单微服务(下)
14 0
Spring Cloud【Finchley】实战-02订单微服务(上)
Spring Cloud【Finchley】实战-02订单微服务
8 0
Spring Cloud【Finchley】实战-01注册中心及商品微服务(下)
Spring Cloud【Finchley】实战-01注册中心及商品微服务(下)
9 0
Spring Cloud【Finchley】实战-01注册中心及商品微服务(上)
Spring Cloud【Finchley】实战-01注册中心及商品微服务(上)
12 0
+关注
青夜之衫
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多