【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注解切面,如需转载请自行联系原博主。
相关文章
|
4天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
21 4
|
6天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
29 6
|
28天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
65 1
|
28天前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
66 0
|
1天前
|
canal 缓存 关系型数据库
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
|
1天前
|
安全 Java Maven
[AIGC] Spring Boot中的切面编程和实例演示
[AIGC] Spring Boot中的切面编程和实例演示
|
13天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
26 2
|
13天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
30 2
从入门到精通:Spring基础注解的全面解析
|
13天前
|
Java 数据库 Spring
切面编程的艺术:Spring动态代理解析与实战
切面编程的艺术:Spring动态代理解析与实战
26 0
切面编程的艺术:Spring动态代理解析与实战
|
17天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0