您的位置:首页 > 编程语言 > Java开发

Spring AOP切面实现:解析

2016-06-30 23:13 471 查看
参考地址:http://my.oschina.net/sniperLi/blog/491854?p={{page}}

AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。

我们现在做的一些非业务,如:日志、事务、安全等都会写在业务代码中(也即是说,这些非业务类横切于业务类),但这些代码往往是重复,复制——粘贴式的代码会给程序的维护带来不便,AOP就实现了把这些业务需求与系统需求分开来做。这种解决的方式也称代理机制。

常见用法:添加统一日志、捕获异常处理后返回统一结果等

AOP的重要概念:

切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”。

连接点(Joinpoint) :程序执行过程中的某一行为,例如,UserService.get的调用或者UserService.delete抛出异常等行为。

通知(Advice) :“切面”对于某个“连接点”所产生的动作。

切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。

目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。

AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true。

通知(Advice)类型:

前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。

后置通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。

环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。

抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。

注:可以将多个通知应用到一个目标对象上,即可以将多个切面织入到同一目标对象。

注解方式解析:

配置文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<context:annotation-config />

<!-- spring扫描注解的配置 -->
<context:component-scan base-package="com.learn" />
<!-- 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面 -->
<aop:aspectj-autoproxy/>
<!-- 切面Bean -->
<bean id="Learn" class="com.learn.spring.Learn" />

</beans>

切面类:
package com.learn.aspect;

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;
import org.springframework.stereotype.Component;

//声明这是一个切面Bean
@Aspect
//声明这是一个组件
@Component
public class LearnAspect {

//配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
@Pointcut("execution(public * com.learn.spring..*.*(..))")
public void pointcut() {
}

/*
* 配置环绕通知,使用在方法pointcut()上注册的切入点
*/
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
//切入点方法调用前自定义行为
System.out.println("around1");

//切入点方法执行
jp.proceed();

//切入点方法调用后自定义行为
System.out.println("around2");
}

/*
* 配置前置通知,使用在方法pointcut()上注册的切入点
*/
@Before("pointcut()")
public void before()
{
System.out.println("before");
}

/*
* 配置后置通知,使用在方法pointcut()上注册的切入点
*/
@After("pointcut()")
public void after()
{
System.out.println("after");
}

/*
*配置后置通知返回,使用在方法pointcut()上注册的切入点
*/
@AfterReturning("pointcut()")
public void afterReturn()
{
System.out.println("afterReturn");
}

/*
* 配置抛出异常后通知,使用在方法pointcut()上注册的切入点
*/
@AfterThrowing(pointcut="pointcut()", throwing="ex")
public void afterThrow(Exception ex)
{
System.out.println("afterThrow "+ex.getMessage());
}
}

注:@AfterThrowing 含有异常参数,必须要在注解中添加声明 throwing="ex" 
否则会报错 java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
java类:
package com.learn.spring;

public class Learn {
public void show()
{
System.out.println("Now is ShowTime");
}

public void showException() throws Exception
{
System.out.println("Now is ShowTime with Exception");
throw new Exception("This is a Exception");
}

}

测试类:
package com.learn.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.learn.spring.Learn;

public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Learn l = (Learn)context.getBean("Learn");
l.show();
l.showException();
}
}
结果:
//正常返回
around1
before
Now is ShowTime
around2
after
afterReturn

//抛出异常
around1
before
Now is ShowTime with Exception
after
afterThrow This is a Exception
Exception in thread "main" java.lang.Exception: This is a Exception
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AOP切面