zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring AOP:基于AspectJ注解开发

SpringAOP开发 基于 注解 AspectJ
2023-06-13 09:20:10 时间
在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。

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

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


定义切面@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 /

关于 execution 中表达式的使用说明,我们在《AspectJ基于XML开发AOP》一节介绍。

定义通知advice

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


@Before( myPointCut() )

public void beforeAdvice(){

}

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


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 >

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

4592.html

springSpring CloudSpring DataSpring frameworkSpring SecuritySpringBootSpringMVC