Spring 之定义切面尝试(基于 XML)
有些场景下只能基于 XML 来定义切面。
【Spring 之定义切面尝试】
1、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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 启用 Aspectj 自动代理 不启动也能用???-->
<aop:aspectj-autoproxy /> <bean id="audience" class="concert.Audience" /> <aop:config>
<aop:aspect ref="audience">
<aop:before method="silenceCellPhones"
pointcut="execution(* concert.Performance.perform(..))" />
<aop:before method="takeSeats"
pointcut="execution(* concert.Performance.perform(..))" />
<aop:after-returning method="applause"
pointcut="execution(* concert.Performance.perform(..))" />
<aop:after-throwing method="demandRefund"
pointcut="execution(* concert.Performance.perform(..))" />
</aop:aspect>
</aop:config> <bean id="theShow" class="concert.TheShow" /> </beans>
使用 <aop:pointcut> 定义命名切点
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 启用 Aspectj 自动代理 不启动也能用???-->
<aop:aspectj-autoproxy /> <bean id="audience" class="concert.Audience" /> <aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* concert.Performance.perform(..))" />
<aop:before method="silenceCellPhones"
pointcut-ref="performance" />
<aop:before method="takeSeats"
pointcut-ref="performance" />
<aop:after-returning method="applause"
pointcut-ref="performance" />
<aop:after-throwing method="demandRefund"
pointcut-ref="performance" />
</aop:aspect>
</aop:config> <bean id="theShow" class="concert.TheShow" /> </beans>
修改为环绕通知:
package concert; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; public class Audience { public void performance() {} public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!AP CLAP!!!AP CLAP!!!AP CLAP!!!");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy /> <bean id="audience" class="concert.Audience" /> <aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* concert.Performance.perform(..))" /> <aop:around method="watchPerformance"
pointcut-ref="performance" />
</aop:aspect>
</aop:config> <bean id="theShow" class="concert.TheShow" /> </beans>
2、测试所定义的切面
package concert; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
// 导入配置
ApplicationContext ctx = new ClassPathXmlApplicationContext("concert-config.xml"); Performance performance = (Performance) ctx.getBean("theShow");
performance.perform();
}
}
一切正常。。。
Spring 之定义切面尝试(基于 XML)的更多相关文章
- Spring 之定义切面尝试(基于注解)
[Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring Aop(七)——基于XML配置的Spring Aop
转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- Spring学习笔记之二----基于XML的Spring AOP配置
在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...
- Spring IOC容器装配Bean_基于XML配置方式
开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...
- 使用Spring框架入门三:基于XML配置的AOP的使用
一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...
随机推荐
- shell学习三十八天----运行顺序和eval
运行顺序和eval shell从标准输入或脚本中读取的每一行称为管道,它包括了一个或多个命令,这些命令被一个或多个管道字符(|)隔开. 其实嗨哟非常多特殊符号可用来切割单个的命令:分号(;),管道(| ...
- Eclipse中的build path详解
http://blog.csdn.net/qqqqqq654/article/details/53043742
- 清空select下拉框的方法
$("#search").find("option").remove(); //或者 $("#search").empty();
- 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈
[BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...
- ES6入门概览二--数组
一 数组 1. Array.from() 将两类对象转为真的数组 : 类似数组的对象(伪数组,如arguments.document.getElementsByTagNames等)和可遍历对象(包括E ...
- python之MySQL学习——输出指定条件的结果集
# 引入pymysql模块 import pymysql as pm # 数据库连接 db = pm.connect(host=",database='task', charset='utf ...
- 第三课补充01——set类型 sorted类型命令操作详解,redis管道及事务
1. set类型的命令操作: (1)sadd命令:向key指定的set集合添加成员 ##sadd命令:是设置set集合类型的数据,sadd <key> <mumber> [& ...
- python学习笔记(二)— 数据类型
一.变量.数据类型 1.计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同 ...
- CMDB初步了解
本节内容 浅谈ITIL CMDB介绍 Django自定义用户认证 Restful 规范 资产管理功能开发 浅谈ITIL TIL即IT基础架构库(Information Technology Infra ...
- Python 模块之 time & datetime
Python 中提供了对时间日期的多种多样的处理方式,主要是在有 time 和 datetime 两个模块. time 在 Python 文档里,time 是归类在 Generic Operating ...