Spring面向切面编程(AOP)方式二

使用注解进行实现:减少xml文件的配置。

1 建立切面类

不需要实现任何特定接口,按照需要自己定义通知。

 1 package org.guangsoft.utils;
 2 import java.util.Date;
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.AfterThrowing;
 6 import org.aspectj.lang.annotation.Around;
 7 import org.aspectj.lang.annotation.Aspect;
 8 import org.aspectj.lang.annotation.Before;
 9 import org.springframework.stereotype.Component;
10 /***
11  * 定义用户的切面类
12  * ***/
13 @Component
14 @Aspect
15 public class UsersAdvice
16 {
17     // 前置的通知
18     @Before("execution(* com.bjsxt.service.impl.*.*(..))")
19     public void before()
20     {
21         System.out.println("before-------------" + new Date());
22     }
23     // 后置通知
24     // @After("execution(* com.bjsxt.service.impl.*.*(..))")
25     @AfterReturning(pointcut = "execution(* com.bjsxt.service.impl.*.*(..))", returning = "rv")
26     public void after(Object rv)
27     {
28         System.out.println("after-------------" + new Date() + "--------------"
29                 + rv);
30     }
31     /****
32      * 环绕通知
33      * **/
34     @Around("execution(* com.bjsxt.service.impl.*.*(..))")
35     public void around(ProceedingJoinPoint jp)
36     {
37         System.out.println("aroun.---------before-------------" + new Date());
38         try
39         {
40             jp.proceed();// 调用目标方法
41         }
42         catch (Throwable e)
43         {
44             e.printStackTrace();
45         } // 调用目标方法
46         System.out.println("around---------after--------------" + new Date());
47     }
48     /***
49      * 异常通知
50      * **/
51     @AfterThrowing(pointcut = "execution(* com.bjsxt.service.impl.*.*(..))", throwing = "ex")
52     public void exception(Throwable ex)
53     {
54         System.out.println("exception-------------------" + ex.getMessage());
55     }
56 }

2xml文件配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans 
 4     xmlns="http://www.springframework.org/schema/beans"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop" 
 7     xmlns:p="http://www.springframework.org/schema/p"
 8     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans
10      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
11      http://www.springframework.org/schema/aop
12      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
13      http://www.springframework.org/schema/context
14      http://www.springframework.org/schema/context/spring-context-4.1.xsd
15      ">
16     <!-- 开启扫描注解 -->
17     <context:component-scan
18         base-package="org.guangsoft.service.impl,org.guangsoft.utils">
19     </context:component-scan>
20     <!-- 开启切面的自动代理 -->
21     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
22 </beans>

在开发中是方式二。减少xml配置,实现灵活

 

原文地址:https://www.cnblogs.com/guanghe/p/6131837.html