AOP基于Aspect注解开发

在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。

为此,AspectJ 框架为 AOP 开发提供了一套注解。AspectJ 允许使用注解定义切面、切入点和增强处理,Spring 框架可以根据这些注解生成 AOP 代理。

关于注解的介绍如表  所示。

表 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>

定义切面@Aspect

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

package net.biancheng;

import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectModule {

}

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


<bean id = "myAspect" class = "net.biancheng.AspectModule">
...
</bean>

定义切入点@Pointcut

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

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

相当于以下代码

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

定义通知advice

@AspectJ 支持 5 种类型的 advice,以下为使用 @Before 的示例。
@Before("myPointCut()")
public void beforeAdvice(){
...
}

示例

下面使用 Eclipse IDE 演示 AspectJ 基于注解开发 AOP,步骤如下:

  1. 创建 SpringDemo 项目,并在 src 目录下创建 net.biancheng 包。
  2. 导入 Spring 相关 JAR 包及 Aspectjrt.jar、Aspectjweaver.jar、Aspectj.jar。
  3. 在 net.biancheng 包下创建 Logging、Man、Beans.xml 和 MainApp。
  4. 运行 SpringDemo 项目。


Logging 类代码如下。


package net.biancheng;

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 类代码如下。

package net.biancheng;

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="net.biancheng.Man">
<property name="name" value="bianchengbang" />
<property name="age" value="12" />
</bean>

<bean id="logging" class="net.biancheng.Logging" />

</beans>

MainApp 类代码如下。

package net.biancheng;

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();
}
}

运行结果如下。

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值