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

Spring AOP总结

2016-04-06 21:08 429 查看
Spring关于切面的编程
1)启用AspectJ,即基于注解的AOP
首先定义一个接口,和它的实现类,将实现类交给IOC去管理,因此需要加上@Component

public interface ArithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);

}

@Component(value="arithmeticCalculator")

public class ArithmeticCalculatorImpl implements ArithmeticCalculator

{

    ...

}

在AspectJ注解中,切面是一个带有@Aspect注解的Java类,其中支持5种类型的通知注解:

@Before:前置通知,在方法执行之前执行

@After:后置通知,在方法执行之后执行

@AfterRunning:返回通知,在方法返回结果之后执行

@AfterThrowing:异常通知,在方法跑出异常之后

@Around:环绕通知,围绕着方法执行

接着上面进行,配置一个切面

@Aspect

@Component

public class LoggingAspect {
@Before("execution(* cn.yxt.study02.ArithmeticCalculatorImpl.*(int, int))")
public void beforeMethod(JoinPoint joinPoint)
{
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();

System.out.println("The method "+methodName+" begins with:"+Arrays.asList(args));
}
//在方法执行之后执行的代码,无论该方法是否出现异常
@After("execution(* cn.yxt.study02.ArithmeticCalculatorImpl.*(int, int))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" end");
}
/**
* 在方法法正常结束受执行的代码
* 返回通知是可以访问到方法的返回值的!
*/
@AfterReturning(value="execution(* cn.yxt.study02.ArithmeticCalculatorImpl.*(int, int))",returning="result")
public void afterReturning(JoinPoint joinPoint,Object result)
{
String methodMethod = joinPoint.getSignature().getName();
System.out.println("The method "+methodMethod+" ends with " + result);
}
//在目标方法出现异常时会执行的代码,可以访问到异常对象;且可以指定在出现特定异常时在执行通知代码
@AfterThrowing(value="execution(* cn.yxt.study02.ArithmeticCalculatorImpl.*(int, int))",throwing="")
public void afterThrowing(JoinPoint joinPoint,Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName + "occurs excetion " + e);

}

配置一个自动扫描的包

<context:component-scan base-package="cn.yxt.study02"></context:component-scan>

要在IOC容器中启用AspectJ注解的支持,需要在配置文件中配置一个元素

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

自动为与AspectJ切面匹配的Bean创建代理

---配置环绕通知

@Around("execution(public int cn.yxt.study02.ArithmeticCalculatorImpl.*(..))")

public Object aroundMethod(ProceedingJoinPoint pjd){    

    Object result = null;

    String methodName = pjd.getSignature().getName();

    try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//执行目标方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;

}

需要将之前方法前面的注解去掉

如果为一个方法定义了多个切面,可以使用@Order注解指定切面的优先级,值越小优先级越高

为了方便,可以将上述每个注解中的execution()提取出来,使用一个切点表达式,下面引用时就方便了

@Pointcut("execution(* cn.yxt.study02.ArithmeticCalculatorImpl.*(int, int))")

public void declareJointPointExpression(){}

引用时只需要@After("declareJointPointExpression()")即可
2)基于XML配置文件
接口、实现类、以及切面Java类编写

配置文件如下:

<!-- 配置bean -->

<bean id="arithmeticCalculator"

    class="cn.yxt.study02.xml.ArithmeticCalculatorImpl"></bean>

<!-- 配置切面的Bean -->

<bean id="loggingAspect"

    class="cn.yxt.study02.xml.LoggingAspect"></bean>

<bean id="validationAspect"

    class="cn.yxt.study02.xml.VlidationAspect"></bean>

<!-- 配置AOP -->

<aop:config>

    <!-- 配置切点表达式 -->

    <aop:pointcut expression="execution(* cn.yxt.study02.xml.ArithmeticCalculator.*(int,int))" id="pointcut"/>

    <!-- 配置切面及通知 -->

    <aop:aspect ref="loggingAspect" order="2">

        <aop:before method="beforeMethod" pointcut-ref="pointcut"/>

        <aop:after method="afterMethod" pointcut-ref="pointcut"/>

        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
   <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>

    </aop:aspect>

    <aop:aspect ref="validationAspect" order="1">
   <aop:before method="validateArgs" pointcut-ref="pointcut"/>

    </aop:aspect>

</aop:config> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring AOP