Spring AOP 基于xml的方式增加五种通知

小编用的IDE是IntelliJ2017,用maven管理项目

 

1.maven仓库依赖

 1 <!-- Spring需要 -->
 2     <dependency>
 3       <groupId>org.springframework</groupId>
 4       <artifactId>spring-context</artifactId>
 5       <version>4.3.9.RELEASE</version>
 6     </dependency>
 7 
 8     <!-- Spring需要 -->
 9     <dependency>
10       <groupId>org.springframework</groupId>
11       <artifactId>spring-core</artifactId>
12       <version>4.3.7.RELEASE</version>
13     </dependency>
14 
15     <!-- aspectj 的jar-->
16     <dependency>
17       <groupId>org.aspectj</groupId>
18       <artifactId>aspectjrt</artifactId>
19       <version>1.8.9</version>
20     </dependency>
21 
22     <!-- aspectj 的jar-->
23     <dependency>
24       <groupId>org.aspectj</groupId>
25       <artifactId>aspectjtools</artifactId>
26       <version>1.8.9</version>
27     </dependency>
28 
29     <!-- aspectj 的jar-->
30     <dependency>
31       <groupId>org.aspectj</groupId>
32       <artifactId>aspectjweaver</artifactId>
33       <version>1.7.4</version>
34     </dependency>
35 
36     <!-- aop需要的jar-->
37     <dependency>
38       <groupId>org.springframework</groupId>
39       <artifactId>spring-aop</artifactId>
40       <version>4.3.9.RELEASE</version>
41     </dependency>
pom.xml

2.创建了接口类

1 package aop.impl;
2 
3 public interface AtithmeticCalculator {
4 
5     int add(int i, int j);
6     int sub(int i, int j);
7     int mul(int i, int j);
8     int div(int i, int j);
9 }
AtithmeticCalculator

3.接口的实现类

 1 package aop.impl;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /*@Component*/
 6 public class AtithmeticCalculatorImpl implements AtithmeticCalculator {
 7     public int add(int i, int j) {
 8         int result = i + j;
 9         return result;
10     }
11 
12     public int sub(int i, int j) {
13         int result = i - j;
14         return result;
15     }
16 
17     public int mul(int i, int j) {
18         int result = i * j;
19         return result;
20     }
21 
22     public int div(int i, int j) {
23         int result = i / j;
24         return result;
25     }
26 }
AtithmeticCalculatorImpl

4.创建实现切面的类

 1 package aop.impl;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.*;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.util.Arrays;
 9 import java.util.List;
10 
11 class LoggingAspect {
12 
13     public void beforeMethod(JoinPoint joinPoint) {
14         //获取当前执行的方法名
15         String methodName = joinPoint.getSignature().getName();
16         //获取当前运行的对象
17         List<Object> args = Arrays.asList(joinPoint.getArgs());
18         System.out.println("前置通知:The method" + methodName + " begins with " + args);
19     }
20 
21 
22     public void afterMethod(JoinPoint joinPoint) {
23         //获取当前执行的方法名
24         String methodName = joinPoint.getSignature().getName();
25         System.out.println("后置通知:The method " + methodName + " ends");
26     }
27 
28     public void afterReturning(JoinPoint joinPoint, Object result) {
29         String methodName = joinPoint.getSignature().getName();
30         System.out.println("返回通知:The method " + methodName + " ends with " + result);
31 
32     }
33 
34 
35     public void afterThrowing(JoinPoint joinPoint, Exception ex) {
36         String methodName = joinPoint.getSignature().getName();
37         System.out.println("异常通知:The method " + methodName + " occurs exception: " + ex);
38     }
39 
40   
41     public Object aroundMethod(ProceedingJoinPoint pjd) {
42         Object result = null;
43         String methodName = pjd.getSignature().getName();
44         //执行目标方法
45         try {
46             //前置通知
47             System.out.println("环绕通知(前置):The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
48             result = pjd.proceed();
49             //后置通知
50             System.out.println("环绕通知(后置)The method ends with "+result);
51         } catch (Throwable e) {
52             e.printStackTrace();
53             System.out.println("环绕通知(异常)The method ocuurs exception: " + e );
54             throw new RuntimeException(e);
55         }
56         System.out.println("环绕通知(后置) The method ends");
57         return result;
58     }
59 }
LoggingAspect

5.配置Bean

 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:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 7 
 8     <!--配置Bean-->
 9     <bean id="atithmeticCalculator" class="aop.impl.AtithmeticCalculatorImpl"></bean>
10       <!--配置切面的bean-->
11     <bean id="loggingAspect" class="aop.impl.LoggingAspect"/>
12 
13     <!--配置AOP-->
14     <aop:config>
15         <!--配置切点表达式-->
16         <aop:pointcut id="pointcut" expression="execution(public int aop.impl.AtithmeticCalculator.*(int,int))"/>
17         <!--配置切面及通知-->
18         <aop:aspect ref="loggingAspect" >
19             <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
20             <aop:after method="afterMethod" pointcut-ref="pointcut"/>
21             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
22             <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
23             <!--<aop:around method="aroundMethod" pointcut-ref="pointcut"/>-->
24         </aop:aspect>
25     </aop:config>
26 </beans>
applicationContext-xml.xml

6.测试类

 1 package aop.impl;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7    public static  void main(String args[]){
 8        //1.创建Spring的IOC容器
 9        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
10        //2.从IOC容器中获取Bean的实例
11        AtithmeticCalculator atithmeticCalculator = context.getBean(AtithmeticCalculator.class);
12        //3.使用Bean
13        int result = atithmeticCalculator.add(3,6);
14        System.out.println("result:"+result);
15 
16        result = atithmeticCalculator.mul(9,8);
17        System.out.println("result:"+result);
18 
19        result  = atithmeticCalculator.div(5,0);
20        System.out.println(result);
21    }
22 }
Main

 

posted @ 2017-09-12 10:37  Limo1996  阅读(257)  评论(0)    收藏  举报