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 ...
随机推荐
- 如何在ChemDraw 15.1 Pro中添加模板
ChemDraw化学绘图工具为了方便用户的使用,特地开发了众多的各种类型模板.用户在绘制一些简单或复杂的化学结构式时,可以直接从ChemDraw模板库里直接调用使用,虽然ChemDraw模板非常的丰富 ...
- MYSQL5.7:几个简单的show语句演示
- JUnit小记
一.参数测试 /** * 1.更改测试运行器为RunWith(Parameterized.class) * 2.声明变量用来存放预期值与结果值 * 3.为测试类声明一个带有参数的公共构造方法,并在其中 ...
- MySQL设置密码的三种方法
其设置密码有三种方法: a. ./mysqladmin -u root -p oldpassword newpasswd(记住这个命令是在/usr/local/mysql/bin中外部命令) b. S ...
- 桥梁模式(Bridge)
桥梁模式属于结构类的设计模式,示意结构图如下:
- Spinner --- 功能和用法
第一种方法: 使用Spinner时需要配置选项的资源文件,资源文件为一个string类型的数组 在res下的values文件夹下新建一个xml文件 内容为: <?xml version=&quo ...
- SQL Server数据库实例名与服务器名不一致的解决办法
SQL Server数据库实例名与服务器名不一致的解决办法 --EXEC sp_addlinkedserver -- @server = 'PSHGQ' --GO --select * from ...
- [USACO5.5]隐藏口令Hidden Password
题目链接:传送门 题目大意:给你一个长度 N 的字符串,5<=N<=5,000,000,将首尾合并成环,断环成链并满足字典序最小,输出此时首字母在原串中的位置-1: 题目思路:最小表示法 ...
- resolution will not be reattempted until the update interval of vas has elap
转自:http://kia126.iteye.com/blog/1785120 maven在执行过程中抛错: 引用 ... was cached in the local repository, re ...
- x-webkit-speech语音搜索
如果你没看到语音图标说明你的浏览器不支持x-webkit-speech 换个chrome你会发现输入框的右侧出现语音小图标.