1.Spring AOP
1.1AOP术语
1.1.1AOP
AOP是Aspect Oriented Programming的简写,意思为面向方向编程或面向切面。该技术将两个类相同的部分(即横切关注点)抽取出来,使得开发者只要专注于开发不同的部分,如:关注点的分离,使得解决特定问题的代码从业务逻辑中独立出来,业务逻辑中的代码不再含有针对特定问题的代码的调用,这部分特定的代码和业务逻辑的关系通过切面来封装和维护,就像抽象类一样。
1.1.2横切关注点
横切关注点通俗的讲就多个类都有的代码,即公共代码。如业务类中,真正解决问题的代码就是那么一两行,但是每次解决特定问题前都有很多相同的配置、声明代码。为了便于维护和编程,这时候就要用到AOP,即切面编程。
1.1.3切面
切面(Aspect)指的是横切关注点的抽象,编写时与类类似,作用与过滤器类似,对特定的类在执行前或执行后添加代码。
1.1.4连接点
连接点(JoinPoint)指那些被拦截到的点,在Spring中这些点指的是方法。通俗的将就是我们要在其前后添加代码的点。
1.1.5通知
通知(Advice)指拦截到连接点之后所要做的事情。
1.1.6切入点
切入点(Pointcut)指我们要对哪些连接点进行拦截的定义,包括切入点表达式和切入点声明两部分。即我们要声明连接点和编写通知。
1.1.7目标对象
目标对象(Target)切面应用到的对象,类似被过滤的对象。
1.1.8代理对象
代理对象(Proxy)切面应用到目标对象之后AOP框架会创建一个对应目标对象的代理对象来实现切面。
1.1.9织入
织入(Weave)指将切入面代码插入到目标对象并导致代理对象创建的过程。
2.实验步骤
a、创建一个java项目;
b、导入架包
c、定义切面
d、创建业务接口和实现类
e、创建Spring配置文件,引入aop命名空间,并定义业务Bean和切面Bean
f、创建测试类
3.核心代码分析
3.1切面
3.1.1代码
package com.sise.ye.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 项目名称:javaII-14
* 包名:com.sise.ye.aop
* 类名:MyInterceptor.java
* 创建人:叶晓东
* 创建时间:2014-5-29
* 描述:AOP类(切面)
* 备注:
* @version 1.0
*/
//定义切面
@Aspect
public class MyInterceptor {
//定义切入点:定义切入点表达式及声明切入点
@Pointcut("execution(* com.sise.ye.service.impl.PersonServiceBean.*(..))")
private void anyMethode(){}
//定义前置通知
@Before("anyMethode()")
public void doBeforeMethod(){
System.out.println("前置通知");
}
//定义后置通知
@AfterReturning("anyMethode()")
public void doAfterReturning(){
System.out.println("后置通知");
}
//定义异常通知
@AfterThrowing("anyMethode()")
public void doAfterThrowing(){
System.out.println("异常通知");
}
//定义最终通知
@After("anyMethode()")
public void doAfter(){
System.out.println("最终通知");
}
//定义环绕通知
@Around("anyMethode()")
public Object doAround(ProceedingJoinPoint pjp)throws Throwable{
//if(){//判断用户是否有权限
System.out.println("进人方法");
Object result=pjp.proceed();//必须调用该方法,实现执行权限在切面中的传递,直到最后传给业务方法
System.out.println("退出方法");
//}
return result;
}
}
3.1.2分析
3.1.2.1@Aspect
@Aspect 作用定义该类为切面类。
3.1.2.2@Pointcut
@Pointcut("execution(* com.sise.ye.service.impl.PersonServiceBean.*(..))") 作用是定义切入点,指明要对那些连接点进行拦截;
第一个*是返回方法的类型,表示所有;
最后括号中的内容是参数,..表示任意参数;
.*(..))前面的表示类,如上面的代码表示PersonServiceBean类,如果是..则表示该包下com.sise.ye.service.impl的所有包及子包中的任何类。
3.1.2.3@Before、@AfterReturning、@AfterThrowing、@After、@Around
@Before 前置通知
@AfterReturning 后置通知
@AfterThrowing 异常通知
@After 最终通知
@Around 环绕通知(判断用户是否有权限)
3.2Spring配置文件
3.2.1代码
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>
<!-- 开启@Aspectj支撑 -->
<aop:aspectj-autoproxy/>
<!-- 定义切面bean -->
<bean id="myInterceptor" class="com.sise.ye.aop.MyInterceptor"/>
<bean id="personService" class="com.sise.ye.service.impl.PersonServiceBean"/>
</beans>
3.2.2代码分析
3.2.2.1<aop:aspectj-autoproxy/>
使用AOP时必须开启@Aspectj支撑
3.3结果图
3.4代码下载:
http://download.csdn.net/detail/yy228313/74609494.总结
使用Spring中AOP使得业务处理等代码更容易管理,方便版本的升级,降低耦合度。