( 十八 ) Spring AOP:基于AspectJ注解开发

( 十八 ) Spring AOP:基于AspectJ注解开发

 

 

1、简介

     在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
为此,AspectJ 框架为 AOP 开发提供了一套注解。AspectJ 允许使用注解定义切面、切入点和增强处理,Spring 框架可以根据这些注解生成 AOP 代理。

关于注解的介绍如表 1 所示:

表 1 Annotation 注解介绍
名称说明
@Aspect 用于定义一个切面。
@Pointcut 用于定义一个切入点。
@Before 用于定义前置通知,相当于 BeforeAdvice。
@AfterReturning 用于定义后置通知,相当于 AfterReturningAdvice。
@Around 用于定义环绕通知,相当于MethodInterceptor。
@AfterThrowing 用于定义抛出通知,相当于ThrowAdvice。
@After 用于定义最终final通知,不管是否异常,该通知都会执行。
@DeclareParents 用于定义引介通知,相当于IntroductionInterceptor(不要求掌握)。


启用 @AspectJ 注解有以下两种方法:

(1)使用 @Configuration 和 @EnableAspectJAutoProxy 注解

@Configuration 
@EnableAspectJAutoProxy
public class Appconfig {
}

(2)基于XML配置

在 XML 文件中添加以下内容启用 @AspectJ。

<aop:aspectj-autoproxy>

 

2、定义切面@Aspect

AspectJ 类和其它普通的 Bean 一样,可以有方法和字段,不同的是 AspectJ 类需要使用 @Aspect 注解,如下所示。

import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}

 

AspectJ 类也可以像其它 Bean 一样在 XML 中配置,如下:

<bean id = "myAspect" class = "com.dw.study.AspectModule">
   ...
</bean>

 

3、定义切入点@Pointcut

@Pointcut 注解用来定义一个切入点,如下:

// 要求:方法必须是private,返回值类型为void,名称自定义,没有参数
@Pointcut("execution(*com.dw.study.*.*(..))")
private void myPointCut() {
}

相当于以下代码

<aop:pointcut expression="execution(* com.dw..*.*(..))"  id="myPointCut"/>

 

4、定义通知advice

@AspectJ 支持 5 种类型的 advice,以下为使用 @Before 的示例:

@Before("myPointCut()")
public void beforeAdvice(){
    ...
}

 

5、示例

Logging 类代码如下:

import org.aspectj.lang.annotation.After;
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;
@Aspect
public class Logging {
    /**
     * 定义切入点
     */
    @Pointcut("execution(* net.biancheng.*.*(..))")
    private void selectAll() {
    }
    /**
     * 前置通知
     */
    @Before("selectAll()")
    public void beforeAdvice() {
        System.out.println("前置通知");
    }
    /**
     * 后置通知
     */
    @After("selectAll()")
    public void afterAdvice() {
        System.out.println("后置通知");
    }
    /**
     * 返回后通知
     */
    @AfterReturning(pointcut = "selectAll()", returning = "retVal")
    public void afterReturningAdvice(Object retVal) {
        System.out.println("返回值为:" + retVal.toString());
    }
    /**
     * 抛出异常通知
     */
    @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
    public void afterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("这里的异常为:" + ex.toString());
    }
}

 

Man 类代码如下:

public class Man {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void throwException() {
        System.out.println("抛出异常");
        throw new IllegalArgumentException();
    }
}

Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy /> <bean id="man" class="com.dw.study.Man"> <property name="name" value="dwTest" /> <property name="age" value="12" /> </bean> <bean id="logging" class="com.dw.study.Logging" /> </beans>

 

MainApp 类代码如下

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Man man = (Man) context.getBean("man"); man.getName(); man.getAge(); man.throwException(); } }

 

运行结果如下:

前置通知
后置通知
返回值为:dwTest
前置通知
后置通知
返回值为:12
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException

 

 

posted @ 2021-07-29 22:41  邓维-java  阅读(139)  评论(0编辑  收藏  举报