概述
在软件开发中,我们重点关注的是业务逻辑代码,但在实际开发中,需要写的代码却不仅仅是业务逻辑,还需要处理记录日志,异常处理,事务控制等一些与业务无关的事情。而且这些代码也是服务端必须的,类似这样的代码分散在系统中的各个地方,如:几乎所有的重要操作方法前面都会加上日志记录代码,这样的代码写起来繁琐,又占用开发时间和精力,而且不容易维护。我们统一把这类代码成为【切面代码】,如何让我们从这些繁琐的工作中抽身而退,更加专注于业务逻辑,这就需要用到Spring的AOP技术。
AOP原理:将复杂的需求分解成不同的方面,将散落在系统中的公共功能集中解决,如下图所示:
通知(Advice)的分类
分类如下:
准备工作
AOP需要的jar包
除Spring必备的五个jar包外,还需要以下三个来支撑AOP:
- aopalliance-1.0.jar
- aspectjweaver-1.5.3.jar
- spring-aop-4.0.6.RELEASE.jar
定义一个接口和实现类
如下所示:
IStudentService接口 代码如下:
StudentServiceImpl类 代码如下:
前置通知
1. 实现接口
前置通知类,需要实现【MethodBeforeAdvice】接口中的before方法,如下所示:
Method method 表示执行的目标方法
Object[] args 表示传入的参数数组
Object target 表示目标对象,即切入点所示的对象
- import java.lang.reflect.Method;
- 4
- org.springframework.aop.MethodBeforeAdvice;
- 6
- 7 class LogBefore MethodBeforeAdvice {
- 8
- 9 /***
- 10 * 前置通知
- * method:表示调用的方法,即切入点
- * args:表示调用方法的参数
- 13 * target:表示方法所在的目标对象
- 14 15 16 void before(Method method,Object[] args,Object target) throws Throwable {
- 17 18 System.out.println("前置通知。。。"19 System.out.println("method="+method+",args数量="+args.length+",target="+target);
- 20 21 }
2. 配置applicationContext.xml文件
如果要支持AOP,需要引入命名空间,如下所示:
- 1 <?xml version="1.0" encoding="UTF-8"?>
- 2 <beans xmlns="http://www.springframework.org/schema/beans"
- 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 4 xmlns:p="http://www.springframework.org/schema/p"
- 5 xmlns:aop="http://www.springframework.org/schema/aop"
- 6 xsi:schemaLocation="http://www.springframework.org/schema/beans
- 7 http://www.springframework.org/schema/beans/spring-beans.xsd
- 8 http://www.springframework.org/schema/aop
- 9 http://www.springframework.org/schema/aop/spring-aop.xsd">
3. 配置两个类对应的bean
- <!-- 服务类 -->
- bean id="studentService" class="com.hex.second.StudentServiceImpl"></bean>
- 3 前置通知类 4 ="logBefore"="com.hex.second.LogBefore"4. 配置AOP
通过AOP配置,将通知类和业务逻辑类进行关联,说明如下:
一个配置文件中,可以有多个<aop:config>配置,每一个aop:config中只能有一个aop:pointcut配置,如果有多个切入点需要配置expression,且切入点必须是全路径配置。如下所示:
将addStudent和通知进行关联 aop:config> 每一个config只有一个poingcut,如果有多个,则需要配置多个config --> 配置通知 aop:advisor advice-ref pointcut-ref/> </>后置通知
1. 实现接口
需要实现【AfterReturningAdvice】接口【afterReturning】方法中的 如下所示:
org.springframework.aop.AfterReturningAdvice;
* 通过实现接口将普通类变成后置通知
9 11 class LogAfter AfterReturningAdvice { 13* 后置通知实现类
* returnValue:返回值
18 void afterReturning(Object returnValue,Method method,1)">24 System.out.println("后置通知。。。"25 System.out.println("returnValue="+returnValue+",method="+method+",1)">26 27 28 }2. 配置切入点和通知的Bean
="logAfter"="com.hex.second.LogAfter"3. AOP配置多个切入点用or连接,多个通知就配置多个aop:advisor
2 3 4 5 ="execution(public void com.hex.second.StudentServiceImpl.deleteStudent(int)) or execution(public void com.hex.second.StudentServiceImpl.addStudent(com.hex.second.Student))" 6 7 8 9 异常通知1. 实现接口
必须以固定格式实现方法:public void afterThrowing([Method,args,target],ThrowableSubclass);
org.springframework.aop.ThrowsAdvice;
* 异常通知
class LogException ThrowsAdvice { 13* 异常通知执行
method 切入点
args 参数个数
target 调用目标对象
ex 异常
afterThrowing(Method method,Object target,Exception ex){
22 System.out.println("异常通知。。。"23 System.out.println("method="+method+",target="+target+",ex="+ex); 24 25 }2. 配置Bean类
="logException"="com.hex.second.LogException"3. 配置AOP如下所示:参数只需要写参数类型即可,不需要写参数名称
可以配置aop:config 3 ="execution(public void com.hex.second.StudentServiceImpl.updateStudent(int))"="pc1"4 5 6 环绕通知1. 实现接口
org.aopalliance.intercept.MethodInterceptor;
org.aopalliance.intercept.MethodInvocation;
5* 环绕通知
class LogAround MethodInterceptor {*
16 public Object invoke(MethodInvocation invocation) 19 Object obj = null20 try { 21 22 前置通知 27 obj = invocation.proceed(); 28 后置通知 29 System.out.println("环绕实现后置通知。。。"30 } catch (Exception e) { 31 异常通知 32 System.out.println("环绕实现异常通知。。。"33 throw e;}
35 36 return obj; 37 38 39 }2. 配置Bean
="logAround"="com.hex.second.LogAround"</bean3. 配置AOP所有配置切入点通知的方式都是一样的。如下所示:
2 ="pc2"3 >
org.springframework.context.ApplicationContext;
org.springframework.context.support.ClassPathXmlApplicationContext;
class TestMain { 7 8 static main(String[] args) { 9 TODO Auto-generated method stub 10 通过Spring进行注入,Spring上下文对象 11 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"12 IStudentService studentService=(IStudentService)context.getBean("studentService"13 Student student =new Student(); 14 studentService.addStudent(student); 15 studentService.deleteStudent(1); 16 studentService.updateStudent(0); 18 19 } View Code
备注
合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。
猜你在找的Spring相关文章