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 }

结果:

原文地址:https://www.cnblogs.com/kill-9/p/9648207.html

时间: 2024-08-30 04:58:44

xml实现的spring-aop的相关文章

spring 使用XML配置开发Spring AOP

XML方式开发AOP与注解开发原理是相同的,所以这里主要介绍一些用法即可.这里需要在XML中引入AOP的命名空间,所以先来了解一下AOP可配置的元素 代码清单:切面类 package com.ssm.chapter11.xml.aspect; public class XmlAspect { public void before() { System.out.println("before ......"); } public void after() { System.out.pri

关于 Spring AOP (AspectJ) 你该知晓的一切

[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian的博客] 关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容

关于 Spring AOP (AspectJ) 该知晓的一切

关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容,尽力呈现出简单易懂的文字含义,如文中有错误请留言,谢谢. OOP的新生机 OOP新生机前夕 神一样的AspectJ-AOP的领跑者 AspectJ的织入方式及其原理概要 基于Aspect Spring AOP

Spring Aop实例之xml配置

上篇博文<3幅图让你了解Spring AOP>中介绍了aop通知类型,AOP的配置方式有2种方式:xml配置和AspectJ注解方式.今天我们就来实践一下xml配置方式. http://blog.csdn.net/xiaoxian8023/article/details/17258933 我采用的jdk代理,所以首先将接口和实现类代码附上 [java] view plaincopy package com.tgb.aop; public interface UserManager { publ

Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法

导包等不在赘述: 建立一个接口:ArithmeticCalculator,没有实例化的方法: package com.atguigu.spring.aop.impl.panpan; 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); } 建立一个类:A

Spring AOP 在XML中声明切面

转载地址:http://www.jianshu.com/p/43a0bc21805f 在XML中将一个Java类配置成一个切面: AOP元素 用途 <aop:advisor> 定义AOP通知器 <aop:after> 定义一个后置通知(不管目标方法是否执行成功) <aop:after-returning> 定义AOP返回通知 <aop:after-throwing> 定义AOP异常通知 <aop:around> 定义环绕通知 <aop:as

Spring AOP基于xml配置实例

目录层级: AOP相关的几个类就是com.aop.xmltype这个报下的4个类. ICalculatorxml.java package com.aop.xmltype; /** * 加减乘除接口,用于AOP测试 * * @author Wei * */ public interface ICalculatorxml { /** * 加法 * * @param a * @param b * @return a+b */ public int doAdd(int a, int b); /** *

(一)Spring AOP:基于XML配置文件

Spring两大重要特性之一就是面向切面编程,下面的例子就是基于XML配置文件最简单的Spring AOP,AOP中的一些术语我就不说了,还是直接操作来的直观 一.maven依赖 <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.2.RE

菜鸟学习Spring——60s配置XML方法实现简单AOP

一.概述. 上一篇博客讲述了用注解的形式实现AOP如今讲述第二种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作參照上一篇博客<菜鸟学习Spring--60s使用annotation实现简单AOP> 文件夹结构: 事实上比起上一篇博客中用annotation来实现AOP的方式我们仅仅要把SecurityHandler.java和配置文件applicationContext.xml更改为以下内容就能够了.以下我把这两个文件的代码写下来. SecurityHandler.java

Spring AOP配置-xml

基于xml的spring AOP配置主要有几个步骤: 1.创建切面类 编写自定义增强代码(如事务处理,日志等) 2.创建service 提供连接点 3.配置切面 在配置之前,先了解一些专业术语 连接点:被拦截的方法 切入点:拦截规则(符合规则被拦截的一类方法) 通知/增强:对拦截的方法添加自定义功能 切面:就是切面类,在其中自定义通知 编写切面类 //切面类 public class AspectClazz { public void save() { System.out.println("保