1.spring aop的相关概念
Aspect切面:横切多个对象。就是横插入系统的功能,例如日志、事务、安全验证等。
Joinpoint连接点:程序执行中某个特定的点,如某个业务方法。Spring AOP只支持在方法调用和异常抛出中插入方面代码。
Advice通知:在切面某个特定的连接点上执行的动作。是编写实际功能代码的地方。
before:在方法前切入;
after:在方法后切入,抛出异常时也会切入;
after-returning:在方法返回后切入,抛出异常不会切入;
after-throwing:在方法抛出异常时切入;
Around Advice:在方法执行前后切入,可以中断或忽略原有流程的执行;
Pointcut切入点:Jointpoint的表达式,表示拦截哪些方法。一个Pointcut对应多个Joinpoint;定义通知应该织入到哪些连接点上。通常切入点指的是类或方法名。例如某个通知要应用所有以abc开头的方法中,那么所有满足这个规则的方法都是切入点。
Introduction引用:在不修改类代码的前提下,为类添加新的方法和属性。
Taget目标对象:被一个切面或者多个切面所通知的对象。
Aop Proxy代理:aop是通过代理对象来访问目标对象,自然你在代理对象中就可以加入你插入的语句。
Weaving织入:在目标对象中插入方面代码的过程就叫做织入。
2.spring aop的xml配置
//将要作为切面的类
public class MoocAspect {
public void before() {
System.out.println("MoocAspect before.");
}
public void afterReturning() {
System.out.println("MoocAspect afterReturning.");
}
public void afterThrowing() {
System.out.println("MoocAspect afterThrowing.");
}
public void after() {
System.out.println("MoocAspect after.");
}
public Object around(ProceedingJoinPoint pjp) {
Object obj = null;
try {
System.out.println("MoocAspect around 1.");
obj = pjp.proceed();
System.out.println("MoocAspect around 2.");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
public Object aroundInit(ProceedingJoinPoint pjp, String bizName, int times) {
System.out.println(bizName + " " + times);
Object obj = null;
try {
System.out.println("MoocAspect aroundInit 1.");
obj = pjp.proceed();
System.out.println("MoocAspect aroundInit 2.");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
}
具体的业务逻辑类(目标类)
public class AspectBiz {
public void biz() {
System.out.println("AspectBiz biz.");
// throw new RuntimeException();
}
public void init(String bizName, int times) {
System.out.println("AspectBiz init : " + bizName + " " + times);
}
}
xml中的配置将我的两个类加入ioc容器,第一个为切面类,第二个为目标类。
<bean id="moocAspect" class="com.imooc.aop.schema.advice.Aspect"></bean>
<bean id="aspectBiz" class="com.imooc.aop.schema.advice.biz.AspectBiz"></bean>
(1)Aspect切面
<aop:config>
<aop:aspect id="MoocAspect" ref="moocAspect "></aop:aspect>
</aop:config>
(2)切入点Pointcut xml配置
<aop:config>
<aop:aspect id="MoocAspect" ref="moocAspect ">
<aop:pointcut id="moocPiontcut" expression="com.imooc.aop.schema.advice.biz.AspectBiz.init() ">
</aop:aspect>
</aop:config>
下面是附带匹配你的方法(连接点)的方式
method:指你要执行的有效代码。pointcut-ref:指定你的切入点
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
<aop:before method="before" pointcut-ref="moocPiontcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut"/>
<aop:after method="after" pointcut-ref="moocPiontcut"/>
<aop:around method="around" pointcut-ref="moocPiontcut"/>
<aop:around method="aroundInit" pointcut="execution(*com.imooc.aop.schema.advice.biz.AspectBiz.init(String, int)) and args(bizName, times)"/>
</aop:aspect>
</aop:config>
3.spring aop api的介绍以及spring aop代理
(1)spring aop api
注意:以下接口方法中参数中的method表示处于切点的方法,args表示这些方法的参数值(可以在这儿改变参数值),target表示这些方法所在的类的对象实例。
实现前置通知的接口 MethodBeforeAdvice 此接口方法如下
- public void before(Method method, Object[] args, Object target)
- throws Throwable { }//在此方法内进行日志..的记录
实现方法返回后通知 AfterReturningAdvice,此接口方法如下
- public void afterReturning(Object returnValue, Method method,
- Object[] args, Object target) throws Throwable { }//在此方法内进行日志..的记录
实现环绕通知 MethodInterceptor,此接口方法如下
- public Object invoke(MethodInvocation invocation) throws Throwable {
- invocation.proceed(); //调用被拦截的方法
- }
- public void afterThrowing(Method method, Object[] args, Object target,
- Throwable exeptionClass) { }//在此方法内进行日志..的记录
(2)spring aop代理和PointcutAdvisorAdvisor
AOP即Aspect-Oriented Programming,面向方面编程。AOP和OOP类似,也是一种编程模式。但是AOP并不能取代OOP,它只是对OOP的扩展和补充。Spring AOP是基于AOP编程模式的一个框架,它实现了AOP范围内的大多数功能,包括Advice、Pointcut等。
AOP典型的应用场景:
2.监控部分重要函数,若抛出指定的异常,需要以短信或邮件方式通知相关人员
3.金控部分重要函数的执行时间
事实上,以上需求没有AOP也能搞定,只是在实现过程中比较郁闷摆了。
1.需要打印日志的函数分散在各个包中,只能找到所有的函数体,手动添加日志。然而这些日志都是临时的,待问题解决之后应该需要清除打印日志的代码,只能再次手动清除^_^!
2.类似1的情况,需要捕获异常的地方太多,如果手动添加时想到很可能明天又要手动清除,只能再汗。OK,该需求相对比较固定,属于长期监控的范畴,并不需求临时添加后再清除。然而,客户某天要求,把其中20%的异常改为短信提醒,剩下的80%改用邮件提醒。改之,两天后,客户抱怨短信太多,全部改成邮件提醒...
3.该需求通常用于监控某些函数的执行时间,用以判断系统执行慢的瓶颈所在。瓶颈被解决之后,烦恼同情况1
其实说白了就是给代码解耦,如果要将日志的逻辑添加到要插入的函数内,以后要更改或者删除的话,因为违背设计模式的开闭原则,改代码对程序员来说会很累很痛苦。
OOP术语:
方面(Aspect):相当于OOP中的类,就是封装用于横插入系统的功能。例如日志、事务、安全验证等。
通知(Advice):相当于OOP中的方法,是编写实际功能代码的地方。
连接点(Joinpoint):程序执行过程中插入方面的地方。Spring AOP只支持在方法调用和异常抛出中插入方面代码。
切入点(Pointcut):定义通知应该织人到哪些连接点上。通常切入点指的是类或方法名。例如某个通知要应用所有以abc开头的方法中,那么所有满足这个规则的方法都是切入点。
目标(Target):目标类或者目标接口。
代理(Proxy):AOP工作时是通过代理对象来访问目标对象。其实AOP的实现是通过动态代理,离不开代理模式,所以必须要有一个代理对象。
织入(Weaving):在目标对象中插入方面代码的过程就叫做织入。
/
示例工程代码(用了很多《Struts 2 + Spring + Hibernate框架技术与项目实战》这本书的第21章的代码,但是也有所改动):
用MyEclipse建立的一个简单的web工程:
前置通知:
- package com.aop;
- import java.lang.reflect.Method;
- import org.apache.log4j.Logger;
- import org.springframework.aop.MethodBeforeAdvice;
- public class BeforeLogAdvice implements MethodBeforeAdvice {
- private Logger logger = Logger.getLogger(BeforeLogAdvice.class);
- public void before(Method method, Object[] args, Object target)
- throws Throwable {
- String targetClassName = target.getClass().getName();
- String targetMethodName = method.getName();
- // args[0] = "Juve";//可以改变参数
- String logInfoText = "前置通知:" + targetClassName + "类的"
- + targetMethodName + "方法开始执行";
- logger.info(logInfoText);
- }
- }
前置通知用于将方面代码插入方法之前,也就是说,在方法执行之前,会首先执行前置通知里的代码。包含前置通知代码的类就是方面,这个类需要实现org.springframework.aop.MethodBeforeAdvice接口。该接口中的before方法如上,参数中的method表示处于切点的方法,args表示这些方法的参数值(可以在这儿改变参数值),target表示这些方法所在的类的对象实例。
后置通知:
- package com.aop;
- import java.lang.reflect.Method;
- import org.apache.log4j.Logger;
- import org.springframework.aop.AfterReturningAdvice;
- public class AfterLogAdvice implements AfterReturningAdvice {
- private Logger logger = Logger.getLogger(AfterLogAdvice.class);
- public void afterReturning(Object returnValue, Method method,
- Object[] args, Object target) throws Throwable {
- // 获取被调用的类名
- String targetClassName = target.getClass().getName();
- // 获取被调用的方法名
- String targetMethodName = method.getName();
- // 日志格式字符串
- String logInfoText = "后置通知:" + targetClassName + "类的"
- + targetMethodName + "方法已经执行";
- // 将日志信息写入配置的文件中
- logger.info(logInfoText);
- }
- }
后置通知的代码在调用被拦截的方法后调用。注意afterReturning方法的第一参数returnValue表示被拦截的方法的返回值。
环绕通知:
- package com.aop;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.apache.log4j.Logger;
- public class LogAroundAdvice implements MethodInterceptor {
- private Logger logger = Logger.getLogger(LogAroundAdvice.class);
- public Object invoke(MethodInvocation invocation) throws Throwable {
- // 获取被调用的方法名
- String targetMethodName = invocation.getMethod().getName();
- /*
- invocation.getArguments()[0] = "Alex";//也可以得到或者改变要执行的方法的参数
- */
- long beginTime = System.currentTimeMillis();
- invocation.proceed(); //调用被拦截的方法
- long endTime = System.currentTimeMillis();
- // 日志格式字符串
- String logInfoText = "环绕通知:" + targetMethodName + "方法调用前时间" + beginTime
- + "毫秒," + "调用后时间" + endTime + "毫秒";
- // 将日志信息写入配置的文件中
- logger.info(logInfoText);
- //这儿相当于强行将拦截的方法的返回值设成了null
- return null;
- //return invocation.proceed();
- }
- }
环绕通知能力最强,可以在方法调用前执行通知代码,可以决定是否还调用目标方法。也就是说它可以控制被拦截的方法的执行,还可以控制被拦截方法的返回值。
异常通知:
- package com.aop;
- import org.apache.log4j.Logger;
- import java.lang.reflect.Method;
- import org.springframework.aop.ThrowsAdvice;
- public class ThrowsLogAdvice implements ThrowsAdvice {
- private Logger logger = Logger.getLogger(ThrowsLogAdvice.class);
- public void afterThrowing(Method method, Object[] args, Object target,
- Throwable exeptionClass) {
- // 获取被调用的类名
- String targetClassName = target.getClass().getName();
- // 获取被调用的方法名
- String targetMethodName = method.getName();
- // 日志格式字符串
- String logInfoText = "异常通知:执行" + targetClassName + "类的"
- + targetMethodName + "方法时发生异常";
- // 将日志信息写入配置的文件中
- logger.info(logInfoText);
- }
- /*
- public void afterThrowing(IllegalArgumentException e){
- System.out.println(e.getMessage());
- }
- */
- }
如果在调用方法时发生异常,异常通知类可以提供一个机会来处理所发生的异常。org.springframework.aop.ThrowsAdvice接口中afterThrowing有两种重载形式:
- public void afterThrowing(Throwable e);
- public void afterThrowing(Method method, Object[] args, Object target, Throwable exeptionClass);
第一种重载形式中,e表示方法抛出的异常类型。如果参数e的类型与异常类型不匹配,afterThrowing将不会被调用。如果这两种重载形式同时存在,并且都匹配抛出的异常类型,那么第一种重载形式优先调用。
目标接口IUserService和类UserService:
- package com.service;
- public interface IUserService {
- public void addUser(String name, int age);
- public void deleteUser(String name);
- }
- package com.service;
- public class UserService implements IUserService {
- public void addUser(String name, int age) {
- //省略诸如操作数据库等复杂的逻辑操作
- System.out.println("add user "+ name +" successfully");
- }
- public void deleteUser(String name) {
- //省略诸如操作数据库等复杂的逻辑操作
- System.out.println("deleted one user named " + name);
- throw new RuntimeException("这是特意抛出的异常信息!");
- }
- }
这个工程中用到了log4j,log4j.properties的配置文件内容就不贴出来了。别忘了在WEB-INF里的logs目录下新建几个log文件。
applicationContext.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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="myUserService" class="com.service.UserService"></bean>
- <!-- 定义前置通知 -->
- <bean id="beforeLogAdvice" class="com.aop.BeforeLogAdvice"></bean>
- <!-- 定义后置通知 -->
- <bean id="afterLogAdvice" class="com.aop.AfterLogAdvice"></bean>
- <!-- 定义异常通知 -->
- <bean id="throwsLogAdvice" class="com.aop.ThrowsLogAdvice"></bean>
- <!-- 定义环绕通知 -->
- <bean id="logAroundAdvice" class="com.aop.LogAroundAdvice"></bean>
- <!-- 定义代理类,名 称为myProxy,将通过myProxy访问业务类中的方法 -->
- <bean id="myProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>com.service.IUserService</value>
- </property>
- <property name="interceptorNames">
- <list>
- <value>beforeLogAdvice</value>
- <!-- 织入后置通知 -->
- <value>afterLogAdvice</value>
- <!-- 织入异常通知 -->
- <value>throwsLogAdvice</value>
- <!-- 织入环绕通知 -->
- <value>logAroundAdvice</value>
- </list>
- </property>
- <property name="target" ref="myUserService"></property>
- </bean>
- </beans>
这里目标类和几个通知类的bean定义就不说了,重点看一下id为myProxy的ProxyFactoryBean的代理类的定义,它包含三个属性:
proxyInterface:代理所实现的接口。Spring AOP无法截获未在该属性指定的接口中的方法。
interceptorNames:用于拦截方法的拦截器名,通知类是其中一种类型,也可以是Advisor。
target:目标类。要注意的是一个代理只能有一个target。
主测试类:
- package com.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.service.IUserService;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- IUserService userService = (IUserService) context.getBean("myProxy");
- userService.addUser("ton", 56);
- userService.deleteUser("ton");
- }
- }
可以看到,这儿应用的bean是myProxy,而不是myUserService。运行结果如下:
- [INFO ] [22:08:13] com.aop.BeforeLogAdvice - 前置通知:com.service.UserService类的addUser方法开始执行
- add user ton successfully
- [INFO ] [22:08:13] com.aop.LogAroundAdvice - 环绕通知:addUser方法调用前时间1382969293109毫秒,调用后时间1382969293109毫秒
- [INFO ] [22:08:13] com.aop.AfterLogAdvice - 后置通知:com.service.UserService类的addUser方法已经执行
- [INFO ] [22:08:13] com.aop.BeforeLogAdvice - 前置通知:com.service.UserService类的deleteUser方法开始执行
- deleted one user named ton
- [INFO ] [22:08:13] com.aop.ThrowsLogAdvice - 异常通知:执行com.service.UserService类的deleteUser方法时发生异常
- Exception in thread "main" java.lang.RuntimeException: 这是特意抛出的异常信息!
/
注:还是这个工程,有没有办法让在取service的bean时可以不用代理呢,让AOP的通知在服务调用方毫不知情的下就进行织入呢。答案是使用BeanNameAutoProxyCreator。在applicationContext.xml声明如下bean:
- <bean id="myServiceAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="interceptorNames">
- <list>
- <value>logAroundAdvice</value>
- </list>
- </property>
- <property name="beanNames">
- <value>*Service</value>
- </property>
- </bean>
这个BeanNameAutoProxyCreator的bean中指明上下文中所有调用以Service结尾的服务类都会被拦截,执行logAroundAdvice的invoke方法。同时它会自动生成Service的代理,这样在使用的时候就可以直接取服务类的bean,而不用再像上面那样还用取代理类的bean。
- package com.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.service.IUserService;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- IUserService userService = (IUserService) context.getBean("myUserService");
- userService.addUser("ton", 56);
- userService.deleteUser("ton");
- }
- }
具体的例子以及详情查看 http://tonl.iteye.com/blog/1966075