一、为什么需要AOP
比如我们需要系统的每一个方法都要输出日志信息,那么我们可以写一个日志方法,然后在每一个方法中调用即可。
但是实际上这是不现实的,如果方法修改了,可能需要在每个实现这个功能的地方去重新改动代码。一旦要修改,就要打开所有调用到的地方去修改。
现在我们用AOP的方式可以实现在不修改源方法代码的前提下,可以统一为原多个方法增加横切性质的“通用处理”。
二、什么是AOP
都说AOP好用,那现在我们来谈谈什么是AOP。
AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。本文提供Spring官方文档出处:Aspect Oriented Programming with Spring
从官方文档上摘抄的解释就是:面向方面编程(AOP)是面向对象编程(OOP)补充的另一种提供思考程序结构补充。在OOP中模块化的关键单元是类,而在AOP模块的单位是一个方面。面对关注点,如事务管理跨越多个类型和对象切模块化。(这些关注经常被称为在AOP文学横切关注点。)
相关概念(只需做个大概的了解就好)----来自于官方文档直译(本人英文水平有限。。。):
Aspect:这横切多个对象关心的模块化。事务管理是企业Java应用程序的横切关注点的一个很好的例子。在Spring AOP中,切面可以使用类(基于模式)或@Aspect注解(@AspectJ风格)注解普通班实施。
Join point:程序在执行过程中的一个点,如方法的执行或异常的处理。在Spring AOP中,一个连接点总是代表一个方法的执行。
Advice:在切面的某个特定的动作连接点。不同类型的意见,包括 “around,” “before” and "after"的advice。 (通知的类型将在下面讨论)。许多AOP框架,包括Spring都是以拦截器作为通知模型,去维护一条围绕着一个连接点的拦截器链。
Pointcut:匹配连接点的断言。通知是跟一个切入点表达式,并在运行在切入点匹配的连接点相关联(例如,一个方法的执行要有一个确定的名字)。切入点表达式作为匹配的连接点的概念是重要的对AOP和Spring缺省使用AspectJ切入点表达式语言。
Introduction:声明代表的类型的额外的方法或字段。 Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存。 (介绍被誉为AspectJ的社会类型间的声明。)
Target object:对象由一个或多个方面被建议。也被称作被通知对象。既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理对象。
AOP proxy:AOP框架,以实现切面契约(例如通知方法执行等等)创建的对象。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
Weaving:与连接其他应用程序类型或对象方面来创建一个被通知的对象。这是可以做到在编译时(使用AspectJ编译器,例如),加载时间,或在运行时。 Spring AOP中,像其他纯Java AOP框架,在运行时进行编织。
那问题来了,AOP是在什么时候去改我们的代码的?即给我们加上额外的横切性质的"通用处理"的?
两个时机:
1.在编译java源代码的时候 ----编译时增强
2.在运行时动态地修改类 ----运行时增强(动态代理)
我们的Spring的AOP的实现原理就是基于动态代理。
三、Spring AOP的3种实现方式
如果配置完成出现错误提示信息:
通配符的匹配很全面, 但无法找到元素 'aop:config' 的声明
可以在 spring-mvc.xml中配置文件要加上命名空间:xmlns:aop=“http://www.springframework.org/schema/aop”
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
1.基于xml配置的实现
在spring-mvc.xml中配置
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<aop:config>
<!--定义切面 -->
<aop:aspect id="logAspect" ref="logInterceptor">
<!-- 定义切入点 (配置在com.gray下所有的类j及其子包在调用之前都会被拦截) -->
<aop:pointcut expression="execution(* com.controller..*.*(..))"
id="logPointCut" />
<!--方法执行之前被调用执行的 -->
<aop:before method="before" pointcut-ref="logPointCut" />
<!--方法执行之后被调用执行的 -->
<aop:after method="after" pointcut-ref="logPointCut" />
</aop:aspect>
</aop:config>
切入点表达式expression=”execution(* com.controller…(…))
execution()是最常用的切点函数,其语法如下所示:
整个表达式可以分为五个部分:
1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.controller包、子孙包下所有类的方法。
4、第二个号:表示类名,号表示所有的类。
5、(…):最后这个星号表示方法名,号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
package core.aop.log4j;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@Component
public class LogInterceptor {
private static Logger logger = Logger.getLogger(LogInterceptor.class);
public void before(){
logger.info("start!");
}
public void after(){
logger.info("end!");
}
}
执行控制台输出
2.基于注解的实现
spring-mvc.xml
<aop:aspectj-autoproxy proxy-target-class="true" />
package core.aop.log4j;
import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 基于注解的实现
* @author Administrator
*
*/
@Aspect
@Component
public class LogInterceptor {
private static Logger logger = Logger.getLogger(LogInterceptor.class);
/**
* 定义一个切入点表达式,用来确定哪些类需要代理
* execution(* com.controller..*.*(..))代表com.controller包下所有类的所有方法都会被代理
*/
@Pointcut("execution(* com.controller..*.*(..))")
public void logPointCut() {}
/**
* 前置方法,在目标方法执行前执行
* @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
*/
@Before("logPointCut()")
public void before(){
logger.info("start!");
}
@After("logPointCut()")
public void after(){
logger.info("end!");
}
}
注解启动报错:Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut
本人的JDK是1.7的,
下载了最新版本的aspectjweaver.jar并替换了原来的版本,error at ::0 can’t find referenced pointcut allAddMethod成功解决了,程序正常运行。
如果要使用AspectJ完成注解切面需要注意下面的JDK与AspectJ的匹配:
JDK1.6 —— aspectJ1.6
JDK1.7 —— aspectJ1.7.3+
3.基于自定义注解的实现
spring-mvc.xml
<aop:aspectj-autoproxy proxy-target-class="true" />
新建 自定义注解标签LogTag.java
package core.tag;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogTag {
String log() default "";
}
新建LogInterceptor3.java
package core.aop.log4j;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import core.tag.LogTag;
/**
* 基于自定义注解的实现
* @author Administrator
*/
@Aspect
@Component
public class LogInterceptor3 {
private static Logger logger = Logger.getLogger(LogInterceptor3.class);
@Pointcut("@annotation(core.tag.LogTag)")
public void logPointCut() {}
/**
* 前置方法,在目标方法执行前执行
* @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
*/
@Before("logPointCut()")
public void before(JoinPoint joinPoint){
MethodSignature methodName = (MethodSignature)joinPoint.getSignature();
Method method = methodName.getMethod();
logger.info("LogTag start" +method.getAnnotation(LogTag.class).log());
}
@After("logPointCut()")
public void after(){
logger.info("LogTag end!");
}
}
在控制层需要使用自定义注解的方法 @LogTag(log=“用户列表”)
//用户列表
@RequestMapping(value="users", method=RequestMethod.GET)
//对应的自定义注解,当方法上写这个注解时,就会进入切面类中
@LogTag(log="用户列表")
public String list(Model model){
Map<String, User> users = new HashMap<String, User>();
for (User user : userService.getAllUsers()) {
users.put(user.getUsername(), user);
}
model.addAttribute("users", users);
return "test/list";
}