有些场景下只能基于 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)的更多相关文章

  1. Spring 之定义切面尝试(基于注解)

    [Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...

  2. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  3. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  4. 基于@AspectJ注解配置切面与基于XML配置切面

    1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  6. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  7. Spring学习笔记之二----基于XML的Spring AOP配置

    在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...

  8. Spring IOC容器装配Bean_基于XML配置方式

    开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...

  9. 使用Spring框架入门三:基于XML配置的AOP的使用

    一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...

随机推荐

  1. CSS中的绝对定位(absolute)误区

    这几天在慕课上看视频学习,偶然听到几个老师都说:CSS绝对定位在没有其他有除static定位的包含块的情况下是以body进行定位,如果要想相对当前元素的父元素来定位,父元素一定要设置position: ...

  2. 可以输入也可以下拉选择的select

    网址:http://www.helloweba.com/view-blog-348.html 示例:http://www.helloweba.com/demo/2016/editable-select ...

  3. 【ASK】git使用中出现Permission denied (publickey).

    好久没有用git了,今天突然执行了一下 $git submodule update --init --recursive =============================== 结果出现如下提 ...

  4. spring-redis SortedSet类型成员的过期时间处理

    redis默认是只支持简单key的过期处理的,像SortedSet类型,也是针对整个set的过期处理,不支持对set的某个成员的过期处理: 为了解决这个问题,做法如下: 1.存储key及值信息到red ...

  5. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13840   Ac ...

  6. PDO防止sql注入的机制

    使用PDO訪问MySQL数据库时,真正的real prepared statements 默认情况下是不使用的. 为了解决问题,你必须禁用 prepared statements的仿真效果. 以下是使 ...

  7. JZOJ.5257【NOIP2017模拟8.11】小X的佛光

    Description

  8. C# .net 数组倒序排序

    1.数组方法 Array.Sort(Array Array);  此方法为数组的排序(正序)方法 Array.Reverse(Array Array);  此方法可以将数组中的值颠倒 两个方法结合使用 ...

  9. shell输出颜色

    #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:- echo -e "\033[30m 黑色字 \033[0m" echo -e "\033 ...

  10. Windows数据库定时备份

    首先打开:任务计划程序 右键任务计划程序库,选择创建基本任务 然后即可以按照实际情况逐步进行 直到启动程序--浏览(程序或脚本)时,这里本人导入的是backup.bat文件,文件内容为 @echo 设 ...