使用AspectJ实现AOP
AspectJ:Java 社区里最完整最流行的 AOP 框架. 在 Spring2.0 以上版本中, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP
在 Spring 中启用 AspectJ 注解支持
在之前我们使用springIOC功能的时候配置了依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
- 这个是搭建spring运行环境的依赖,包括IOC和AOP功能。
- 使用AOP时,如果使用xml方式配置,则不需要任何额外的jar包。
- 如果使用注解方式,你就可以在类上直接一个@Aspect就搞定,不用费事在xml里配了。但是这需要额外的jar包( aspectjweaver.jar)。
使用@Aspect注解的方式实现AOP
引入额外的jar包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
这个加入了 aspectjweaver.jar 包,主要是对aspectj注解进行支持。
配置文件加入aop 的命名空间:
- 将 aop Schema 添加到
<beans>
根元素中.
配置启用 AspectJ 注解支持
在Spring IOC 容器中加入:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- 当 Spring IOC 容器侦测到 Bean 配置文件中的
<aop:aspectj-autoproxy>
元素时, 会自动为与AspectJ 切面匹配的 Bean 创建代理.- 被代理对象必须在容器中声明了bean,而且我们也必须通过springIOC获取这个bean,如果是使用直接new出来的,将无效。
AOP前置通知
举个例子,功能要求是,给计算器加上日志:
- 方法调用前,打印出日志
- 方法调用后,打印出日志
- Calculator接口:
package com.spring.aop;
public interface Calculator {
int add(int i,int j);
int sub(int i,int j);
}
- 实现类:
package com.spring.aop;
import org.springframework.stereotype.Component;
@Component
public class CalculatorImpl implements Calculator{
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
}
- 这个就是目标对象计算器类
- 记得给实现类加上注解,让IOC容器管理
- 日志切面:
package com.spring.aop;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 声明该类为切面:
* 1.让IOC容器创建这个类的bean(@Component)
* 2.声明为切面(@Aspect)
*/
@Aspect
@Component
public class LoggingAspect {
//声明这是一个前置通知
@Before("execution(public int com.spring.aop.CalculatorImpl.add(int, int))")
public void beforeMethod(JoinPoint joinPoint){
//注意,JoinPoint来自org.aspectj.lang.JoinPoint,小心导错包
//执行方法名
String methodName = joinPoint.getSignature().getName();
//执行方法参数
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("method 【"+methodName+"】 begin:"+args);
}
}
- 将横切关注点(需要的附加功能)抽取出来模块化为一个切面
- 切面类可以配置多种类型的通知,这里只配置了前置通知,使用@Before标注的方法
- JoinPoint 参数可以让我们访问到连接点的细节,比如目标方法名或者方法参数等。如果不需要,可以不加
- 配置文件:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.spring.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
- 测试类:
package com.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop-aspectj.xml");
Calculator cal = (Calculator) ctx.getBean("calculatorImpl");
int result = cal.add(1,2);
System.out.println(result);
}
}
在执行计算器的add方法前加入了日志输出功能,而我们并没有干涉核心代码,保持了核心代码的纯洁。
- 通知配置相关知识点:
@Before("execution(public int com.spring.aop.CalculatorImpl.add(int, int))")
注解标识在切面方法上,表示在目标对象(add方法)执行前执行这个方法
上面只制定了一个方法.CalculatorImpl类的add方法。
可以使用通配符 * 指定多个方法,如:
@Before("execution(* com.spring.aop.*.*(..))")
- 第一个 * 表示任意类返回值
- 第二个 * 表示com.spring.aop包下面所有包
- 第三个 * 表示包里的任意方法
- (..) 表示匹配任意个参数
后置通知:
将上面的@Before换成@After
- 后置通知, 在方法执行之后执行
- 注意,无论方法是否抛出异常,后置通知都会被执行
- 后置通知不能得到目标方法的返回值,返回值需要在返回通知中才能得到
本系列参考视频教程: http://edu.51cto.com/course/1956.html