上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP

其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行。

例子:这里的例子和上一篇的例子一样。换成xml方式

   //方法接口
package com.atguigu.spring.aop.impl; public interface Calculator { public int add(int i,int j);
public int sub(int i,int j);
public int div(int i,int j);
public int mul(int i,int j);
}
// 方法实现类
package com.atguigu.spring.aop.impl; public class CalculatorImpl implements Calculator{ public int add(int i,int j){
return i+j;
};
public int sub(int i,int j){
return i-j;
};
public int div(int i,int j){
return i/j;
};
public int mul(int i,int j){
return i*j;
}
}

日志切面:

 package com.atguigu.spring.aop.impl;

 import java.util.Arrays;
import java.util.List; public class LoggingAspect {
//前置通知日志日志
public void beforedMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args = Arrays.arrays(joinPoint.getArgs());
System.out.println("The method "+ methodName +"begins: "+args);
}
//返回通知日志
public void afterReturningMethod(Joinpoint joinpoint,Object result){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends with "+result);
}
// 异常通知日志
public void afterThrowingMethod(Joinpoint joinpoint,Exception e){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" occurs excetion "+e);
}
}

验证切面:

 package com.atguigu.spring.aop.impl;

 import java.util.Arrays;
import java.util.List; public class ValidationAspect {
public void beforedMethod(JoinPoint joinPoint){
System.out.println("---->Validation: "+ Arrays.aslist(joinPoint.getArgs));
}
}

main函数

 package com.atguigu.spring.aop.impl;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
Calculator cal = (Calculator) ctx.getBean(Calculator.class);
int result=cal.add(1,2);
System.out.println("result: "+result);
}
}

重点看xml配置:

     <!--先配置要使用的bean和切面bean,让spring容器创建对象-->
<!-- 配置bean -->
<bean id="calculatorimpl" class="com.atguigu.spring.aop.impl.Calculatorimpl"></bean>
<!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.atguigu.spring.aop.impl.LoggingAspect"></bean>
<bean id="validationAspect" class="com.atguigu.spring.aop.impl.ValidationAspect"></bean> <!-- 配置AOP ,表示出对象中的逻辑-->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.impl.CalculatorImpl.*(int, int))"
id="pointcut"></aop:pointcut>
<!-- 配置切面通知 -->
<aop:aspect ref="validationAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
<!-- 配置切面通知 -->
<aop:aspect ref="loggingAspect" order="1">
<!--前置通知 -->
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
<!--返回通知,注意,方法中有多少个参数,这里就要配置多少个参数,参数值和函数中一致-->
<aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result"></aop:after-returning>
<!--异常通知,注意,方法中有多少个参数,这里就要配置多少个参数,参数值和函数中一致-->
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>
</aop:aspect>
</aop:config>

结果和注解配置的一致,但配置比较麻烦,不推荐使用。

Spring学习记录(十三)---基于xml文件配置AOP的更多相关文章

  1. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  2. 用idea 创建一个spring小demo,基于xml文件配置

    1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...

  3. idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题

    一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...

  4. (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置

    要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...

  5. Spring学习笔记之一----基于XML的Spring IOC配置

    1. 在spring配置文件中,如果对一个property进行直接赋值,可使用<value>元素,spring负责将值转化为property指定的类型:也可以直接在property元素上使 ...

  6. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...

  7. springMVC学习记录1-使用XML进行配置

    SpringMVC是整个spring中的一个很小的组成,准确的说他是spring WEB这个模块的下一个子模块,Spring WEB中除了有springMVC还有struts2,webWork等MVC ...

  8. spring +springmvc+mybatis组合applicationContext.xml文件配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. spring +springmvc+mybatis组合mybatis-config.xml文件配置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...

随机推荐

  1. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  2. UWP中实现自定义标题栏

    UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...

  3. nodejs进阶(1)—输出hello world

    下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var  http  =  require ...

  4. Android数据加密之Base64编码算法

    前言: 前面学习总结了平时开发中遇见的各种数据加密方式,最终都会对加密后的二进制数据进行Base64编码,起到一种二次加密的效果,其实呢Base64从严格意义上来说的话不是一种加密算法,而是一种编码算 ...

  5. jQuery学习之路(1)-选择器

    ▓▓▓▓▓▓ 大致介绍 终于开始了我的jQuery学习之路!感觉不能再拖了,要边学习原生JavaScript边学习jQuery jQuery是什么? jQuery是一个快速.简洁的JavaScript ...

  6. JavaScript之链式结构序列化

    一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } swi ...

  7. 解读发布:.NET Core RC2 and .NET Core SDK Preview 1

    先看一下 .NET Core(包含 ASP.NET Core)的路线图: Beta6: 2015年7月27日 Beta7: 2015年9月2日 Beta8: 2015年10月15日 RC1: 2015 ...

  8. golang sync.WaitGroup bug

    注意,这个结构体,要是想在函数之间传来传去的话,必须要使用指针....... 这个结构体里没有 指针,这个类型可以说没有“引用特性”. 被坑了一晚上.特此记录.

  9. CSS中强悍的相对单位之em(em-and-elastic-layouts)学习小记

    使用相对单位em注意点 1.浏览器默认字体是16px,即1em = 16px,根元素设置如下 html{ font-size: 100%; /* WinIE text resize correctio ...

  10. BPM配置故事之案例14-数据字典与数据联动

    小明遇到了点麻烦,他昨天又收到了行政主管发来的邮件,要求把出差申请单改由H3 BPM进行,表单如下 行政主管的出差申请表 小明对表单进行了调整,设计出了一份适合在系统中使用的表单,但在"出差 ...