Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明。
通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的。由于 AspectJ 得到越来越多的 AOP 框架支持 , 所以以注解风格编写的切面将会有更多的重用机会。
<?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"
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.xsd"> <!-- 配置 ArithmeticImpl 的 bean -->
<bean id="arithmetic" class="com.itdoc.spring.aop.cutface.ArithmeticImpl"/> <!-- 配置切面的 bean -->
<bean id="asjectLogging" class="com.itdoc.spring.aop.cutface.AsjectLogging"/>
<bean id="validate" class="com.itdoc.spring.aop.cutface.Validate"/> <!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.itdoc.spring.aop.cutface.*.*(..))"/>
<!-- 配置切面通知 -->
<aop:aspect ref="asjectLogging" order="2">
<aop:around method="around" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
</aop:aspect>
<aop:aspect ref="validate" order="1">
<aop:before method="beforeValidate" pointcut-ref="pointcut"/>
</aop:aspect> </aop:config>
</beans>
package com.itdoc.spring.aop.cutface; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 19:34
*/
public interface Arithmetic { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package com.itdoc.spring.aop.cutface; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-03 19:35
*/
public class ArithmeticImpl implements Arithmetic {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package com.itdoc.spring.aop.cutface; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays; /**
* 通知
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 9:50
*/ public class AsjectLogging { public void afterReturning(JoinPoint joinPoint, Object result) {
Object methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
} public void afterThrowing(JoinPoint joinPoint, Exception e) {
Object methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " exception with " + e);
} public Object around(ProceedingJoinPoint point) {
Object methodName = point.getSignature().getName();
Object[] args = point.getArgs();
Object result = null;
try {
//前置通知
System.out.println("The method " + methodName + " begins with" + Arrays.asList(args));
//执行方法
result = point.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
e.printStackTrace();
//异常通知
System.out.println("The method " + methodName + " exception with " + e);
} finally {
//后置通知
System.out.println("The method " + methodName + " ends");
}
return result;
}
}
package com.itdoc.spring.aop.cutface; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 12:20
*/
public class Validate { public void beforeValidate() {
System.out.println("I am Validate's beforeValidate method...");
}
}
package com.itdoc.spring.aop.cutface; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-03-04 9:54
*/
public class Main { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml");
Arithmetic arithmetic = (Arithmetic) ctx.getBean("arithmetic");
System.out.println("result = " + arithmetic.div(1, 1));
}
}
控制台输出:
I am Validate's beforeValidate method... |
Spring学习--基于 XML 的配置声明切面的更多相关文章
- idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题
一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一 ...
- Spring IoC — 基于XML的配置
1.属性注入 注意点: 1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常. 2)JavaBean关于属性命名的特殊规范.Spring只会检查B ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
随机推荐
- LEAVE TO LIST-PROCESSING
LEAVE SCREEN 現在の画面の処理を中止し.次の画面を呼び出す - LEAVE TO SCREEN 現在の画面の処理を中止し.動的に定義された次の画面を呼び出す - LEAVE [PROGRA ...
- js倒计时页面跳转
HTML: <p><span id="timer">60</span>s 后跳转到百度首页</p> JS: //倒计时方法 func ...
- jenkins安全内容配置策略
有时我们使用HTML Publisher Plugin插件时,在jenkins点开html report,会发现没有带任何的css或js样式,这是因为Jenkins 1.641 / Jenkins 1 ...
- pytest 测试报告
测试报告 运行测试用例后,为了保存结果,我们需要生成测试报告,同时可以把运行的测试报告发送相关人员查阅,这时需要安装一个插件(pytest-html) pytest-html插件安装 pip inst ...
- shiro笔记
控制某一角色拥有此选项 上图 标签为shiro:hasRole表示 此时只有admin角色才拥有 系统用户管理和角色管理两个tab 页 上图 标签为shiro:hasPermission表示 此时只有 ...
- LeetCode 23 ——合并 K 个排序链表
1. 题目 2. 解答 2.1. 方法一 在 合并两个有序链表 的基础上,我们很容易想到第一种解法,首先我们将第一个链表和第二个链表合并成一个新的链表,然后再往后依次合并接下来的每个链表即可. 假设每 ...
- const 常量与 define常量的区别
c++中的常量可以使用const定义,也可以使用#define宏定义的方式:二者区别如下: - **区别** 1. const定义的常量有自己的数据类型,编译器可以对其进行严格的类型检查:但是defi ...
- 以太坊 生成助记词和infuru插件
https://iancoleman.io/bip39/ https://infura.io google faucet : https://faucet.rinkeby.io/ 登录google账号 ...
- java编程思想 内容总结
Java编程思想重点笔记(Java开发必看) Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面 试过程中,而且 ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...