xml实现的spring-aop

Posted kill-9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml实现的spring-aop相关的知识,希望对你有一定的参考价值。

接口:

 1 package spring.aop2;
 2 
 3 public interface Arithmetic {
 4 
 5     Integer add(Integer a, Integer b);
 6     Integer sub(Integer a, Integer b);
 7     Integer div(Integer a, Integer b);
 8 
 9 
10 }

实现:

 1 package spring.aop2;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class ArithmeticImpl implements Arithmetic {
 7 
 8     @Override
 9     public Integer add(Integer a, Integer b) {
10         System.out.println("add -> "+(a+b));
11         return a+b;
12     }
13 
14     @Override
15     public Integer sub(Integer a, Integer b) {
16         System.out.println("sub -> "+(a-b));
17         return a-b;
18     }
19 
20     @Override
21     public Integer div(Integer a, Integer b) {
22         System.out.println("div -> "+(a/b));
23         return a/b;
24     }
25 
26 
27 }

切面类1:

 1 package spring.aop2;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.*;
 5 import org.springframework.core.annotation.Order;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.util.Arrays;
 9 import java.util.List;
10 
11 
12 public class aspect {
13 
14 
15     public void beforeMethod(JoinPoint joinPoint){
16         String methodName = joinPoint.getSignature().getName();
17         List<Object> args = Arrays.asList(joinPoint.getArgs());
18         System.out.println("methodName:"+methodName+"--->before args: "+args);
19     }
20 
21 
22     public void afterMethod(JoinPoint joinPoint){
23         String methodName = joinPoint.getTarget().getClass().getName();
24         List<Object> args = Arrays.asList(joinPoint.getArgs());
25         System.out.println("methodName:"+methodName+"--->after args: "+args);
26     }
27 
28 
29     public void afterReturning(JoinPoint joinPoint,Object result){
30         String methodName = joinPoint.getSignature().getName();
31         System.out.println("methodName:"+methodName+"--->afterReturning args: "+result);
32 
33     }
34 
35     public void afterThrowing(JoinPoint joinPoint,Exception e){
36         String methodName = joinPoint.getSignature().getName();
37         System.out.println("methodName:"+methodName+"--->afterThrowing args: "+e);
38 
39     }
40 }

切面类2:

 1 package spring.aop2;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.core.annotation.Order;
 7 import org.springframework.stereotype.Component;
 8 
 9 import java.util.Arrays;
10 
11 public class VliAspect {
12 
13     public void validateArgs(JoinPoint joinPoint){
14 
15         System.out.println("validateArgs -> "+ Arrays.asList(joinPoint.getArgs()));
16     }
17 }

配置:aop2.xml

 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:p="http://www.springframework.org/schema/p"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 9        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
10 
11     <!--自动为匹配的类生成代理对象-->
12     <aop:aspectj-autoproxy proxy-target-class="true" />
13 
14     <!--配置bean-->
15     <bean id="arithmeticImpl" class="spring.aop2.ArithmeticImpl"/>
16 
17     <!--配置切面bean-->
18     <bean id="vliAspect" class="spring.aop2.VliAspect"/>
19     <bean id="aspect" class="spring.aop2.aspect"/>
20 
21     <!--配置aop-->
22     <aop:config>
23         <!--配置切点表达式-->
24         <aop:pointcut id="pointcut" expression="execution(* spring.aop2.*.*(..))"/>
25         <!--配置切面和通知-->
26         <aop:aspect ref="aspect" order="2">
27             <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
28             <aop:after method="afterMethod" pointcut-ref="pointcut"/>
29             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
30             <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
31 
32         </aop:aspect>
33         <aop:aspect ref="vliAspect" order="1">
34             <aop:before method="validateArgs" pointcut-ref="pointcut"/>
35         </aop:aspect>
36     </aop:config>
37 
38 
39 
40 </beans>

测试:

 1 package spring.aop2;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args){
 9 
10         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:aop2.xml");
11         Arithmetic proxy = ac.getBean("arithmeticImpl",ArithmeticImpl.class);
12         proxy.add(3,5);
13         System.out.println("-----------------------");
14         proxy.sub(3,0);
15         System.out.println("-----------------------");
16         proxy.div(3,0);
17         System.out.println("-----------------------");
18     }
19 }

结果:

技术分享图片

以上是关于xml实现的spring-aop的主要内容,如果未能解决你的问题,请参考以下文章

spring-AOP原理

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

需要示例代码片段帮助

spring-aop

Spring-AOP注解开发

spring-AOP(面向切面编程)