一、编写基本处理方法

package com.kang.sping.xml.aop;

public class Math{
//加
public int add(int n1,int n2){
int result=n1+n2;
System.out.println(n1+"+"+n2+"="+result);
return result;
} //减
public int sub(int n1,int n2){
int result=n1-n2;
System.out.println(n1+"-"+n2+"="+result);
return result;
} //乘
public int mut(int n1,int n2){
int result=n1*n2;
System.out.println(n1+"*"+n2+"="+result);
return result;
} //除
public int div(int n1,int n2){
int result=n1/n2;
System.out.println(n1+"/"+n2+"="+result);
return result;
}
}

二、编写通知类

package com.kang.sping.xml.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 通知类,横切逻辑
*
*/
public class Advices { public void before(JoinPoint jp){
System.out.println("----------前置通知----------");
System.out.println(jp.getSignature().getName());
} public void after(JoinPoint jp){
System.out.println("----------最终通知----------");
} public void afterReturning(JoinPoint joinPoint, Object result){
//定义AfterReturning通知方法
} public void afterThrowing(JoinPoint joinPoint, Exception e){
//定义AfterThrowing通知方法
} public Object around(ProceedingJoinPoint pjd){
//定义Around通知方法
return null;
}
}

三、配置XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 被代理的对象 -->
<bean id="math" class="com.kang.sping.xml.aop.Math"></bean> <!-- 配置切面的 bean.切面 Bean 必须有一个标示符id, 供 <aop:aspect> 元素引用 -->
<bean id="advices" class="com.kang.sping.xml.aop.Advices"></bean>
<!-- 可以定义多个切面bean,例如再定义一个日志bean -->
<!-- <bean id="loggingAspect" class="com.kang.spring.xml.aop.LoggingAspect"></bean> -->
<!-- aop配置 --> <!--
proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。
如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。
如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理。
spring使用aop时需要设置proxy-target-class="true" ,否则无法依赖注入
-->
<!-- 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部 -->
<aop:config proxy-target-class="true"> <!--
配置切点表达式,id标示了该切点以供下面引用。切入点必须定义在 <aop:aspect> 元素下,
或者直接定义在 <aop:config>元素下. 定义在 <aop:aspect> 元素下: 只对当前切面有效。
定义在 <aop:config> 元素下: 对所有切面都有效。
基于 XML的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.
-->
<aop:pointcut expression="execution(* com.kang.sping.xml.aop.Math.*(..))"
id="pointcut1" /> <!--
配置切面 .每个切面而言, 都要创建一个 <aop:aspect> 元素来为具体的切面实现,
用以引用具体bean的方法 这里配置为ref="advices",则可以调用advices对应的类中的方法
-->
<aop:aspect ref="advices"> <!--配置连接通知方法与切点,method 属性指定切面类中通知方法的名称 -->
<aop:before method="before" pointcut-ref="pointcut1" />
<aop:after method="after" pointcut-ref="pointcut1" />
<!--
配置其他通知方法
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut1" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut1" returning="result"/>
<aop:around method="around" pointcut-ref="pointcut1"/>
-->
</aop:aspect> <!--aspect里面有一个order属性,order属性的数字就是横切关注点的优先级顺序 -->
<!--
可以配置多个切面
<aop:aspect ref="loggingAspect" order="1">
<aop:before method="doLogging" pointcut-ref="pointcut1" /> </aop:aspect>
-->
</aop:config> </beans>

四、编写测试方法

package com.kang.sping.xml.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
Math math = ctx.getBean("math", Math.class);
int n1 = 100, n2 = 5;
math.add(n1, n2);
math.sub(n1, n2);
math.mut(n1, n2);
math.div(n1, n2);
} }

五、运行结果

六、总结

正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会。当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema命名空间。

基于XML配置的Sping AOP详解的更多相关文章

  1. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  2. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  3. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

  4. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...

  5. Spring 基于xml配置方式的AOP(8)

    1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...

  6. Sping MVC不使用任何注解处理(jQuery)Ajax请求(基于XML配置)

    1. Spring Spring框架是一个轻量级的解决方案,是一个潜在的一站式商店,用于构建企业就绪的应用程序.Spring框架是一个Java平台,为开发Java应用程序提供全面的基础架构支持.Spr ...

  7. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  8. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  9. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

随机推荐

  1. javaweb学习总结(九)—— 通过Servlet生成验证码图片(转)

    (每天都会更新至少一篇以上,有兴趣的可以关注)转载自孤傲苍狼 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  2. input弹出的手机键盘搜索事件

    一.input的搜索框    在input标签里面把type设置为search就可以了.弹出的手机键盘回车键也会变成搜索或者是搜索的图标. <input id="search" ...

  3. hdu 1796(容斥原理+状态压缩)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. LeetCode OJ--Unique Paths *

    https://oj.leetcode.com/problems/unique-paths/ 首先,转换成一个排列组合问题,计算组合数C(m+n-2) (m-1),请自动想象成上下标. class S ...

  5. Codeforces 934 C.A Twisty Movement-前缀和+后缀和+动态规划

    C. A Twisty Movement   time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. java 判断请求来自手机端还是电脑端

    根据当前请求的特征,判断该请求是否来自手机终端,主要检测特殊的头信息,以及user-Agent这个header public static boolean isMobileDevice(HttpSer ...

  7. 洛谷—— P1849 [USACO12MAR]拖拉机Tractor

    https://www.luogu.org/problemnew/show/P1849 题目描述 After a long day of work, Farmer John completely fo ...

  8. 文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  9. luogu P2158 [SDOI2008]仪仗队

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  10. 【APIO2015】Jakarta Skyscrapers

    题目描述 印尼首都雅加达市有 $N$ 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 $0$ 到 $N − 1$.除了这 $N$ 座摩天楼外,雅加达市没有其他摩天楼. 有 $M$ 只叫做 ...