Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置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的更多相关文章
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- 用idea 创建一个spring小demo,基于xml文件配置
1.首先,File->new->project ,进入新增项目页面 或者在 2.勾选spring,然后点击下一步 3.修改项目名称和项目位置 进入页面后 5.创建一个spring配置文件 ...
- idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题
一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring学习笔记之一----基于XML的Spring IOC配置
1. 在spring配置文件中,如果对一个property进行直接赋值,可使用<value>元素,spring负责将值转化为property指定的类型:也可以直接在property元素上使 ...
- Spring学习记录(九)---通过工厂方法配置bean
1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...
- springMVC学习记录1-使用XML进行配置
SpringMVC是整个spring中的一个很小的组成,准确的说他是spring WEB这个模块的下一个子模块,Spring WEB中除了有springMVC还有struts2,webWork等MVC ...
- spring +springmvc+mybatis组合applicationContext.xml文件配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- spring +springmvc+mybatis组合mybatis-config.xml文件配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...
随机推荐
- document.documentElement.clientHeight 与 document.body.clientHeight(杜绝千篇一律的抄袭!!)
document.documentElement.clientHeight 与 document.body.clientHeight用来获取页面可视高度我觉得有点问题.这两个应该不是一个东西. 页面中 ...
- Android GridView 通过seletor 设置状态和默认状态
Android中可以通过selector控制GridView Item 的状态,而省去使用代码控制 GridView View Selector Xml文件 <?xml version=&quo ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
- c# Enumerable中Aggregate和Join的使用
参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaochen ...
- SharePoint 2016 入门视频教程
之前一直有朋友让自己录一些SharePoint的入门视频,之前没有太多时间,一个巧合的机会收到CSDN学院的邮件,可以在CSDN上发布视频教程,自己就录了一些.说起录视频也是蛮辛苦的,每天下班吃完饭要 ...
- Tomcat 部署我的第一个程序
idea 生成war包.先双击clean,再双击package.生成成功之后就会产生war包. 第二步:将生成好的war文件复制到tomcat文件夹下. 第三步:配置tomcat的server.xml ...
- PMON failed to acquire latch, see PMON dump
前几天,一台Oracle数据库(Oracle Database 10g Release 10.2.0.4.0 - 64bit Production)监控出现"PMON failed to a ...
- 机器学习之sklearn——EM
GMM计算更新∑k时,转置符号T应该放在倒数第二项(这样计算出来结果才是一个协方差矩阵) from sklearn.mixture import GMM GMM中score_samples函数第 ...
- ASP.NET Core MVC 配置全局路由前缀
前言 大家好,今天给大家介绍一个 ASP.NET Core MVC 的一个新特性,给全局路由添加统一前缀.严格说其实不算是新特性,不过是Core MVC特有的. 应用背景 不知道大家在做 Web Ap ...
- 2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...