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();
    }
}

一切正常。。。

时间: 2024-08-29 05:35:11

Spring 之定义切面尝试(基于 XML)的相关文章

Spring IOC的初始化过程——基于XML配置(一)

前言:在面试过程中,Spring IOC的初始化过程,基本上属于必答题,笔者的亲身经历.因此本文基于Spring的源码对其IOC的初始化过程进行总结. 注:spring版本为4.3.0. 1.调试前准备 在spring源码中,添加如下内容(有关spring源码如何导入idea,请查询相关资料): 说明: ①User为简单bean,含有name和gender两个属性. ②User.xml为spring配置文件,仅仅对User进行简单的配置. ③SpringIoCDebug为测试类. 先看执行结果:

Spring Security应用开发(02)基于XML配置的用户登录

1.1. 基于XML配置的登录功能 经过一系列配置之后,可以使用Spring Security内置功能实现最基本的用户登录功能以及角色验证功能,这种内置的功能没有任何实用价值,仅仅用于了解Spring Security的工作方式. (1)配置web.xml. 主要是为Spring MVC和Spring Security提供一些入口,以便有机会进行Spring MVC以及Spring Security的初始化和过滤处理等工作. <servlet> <servlet-name>spri

使用Spring框架入门一:基于XML配置的IOC/DI的使用

一.Spring框架 1.方法一:逐项导入基础依赖包: spring-core.spring-beans.spring-context.spring-expression 2.方法二:最简洁的导入,直接导入spring-context包: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version&g

Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML 定义的 Bean 信息转换为 Spring 框架的 Bean Definition 对象的处理过程,向读者展示了 Spring 框架的奥妙之处,可以加深开发人员对 Spring 框架的理解. 0 评论: 秦 天杰, 软件工程师, IBM China 2013 年 9 月 02 日 内容 在 IBM

Spring @Aspect实现切面编程

参考:http://blog.csdn.net/cdl2008sky/article/details/6268628 Spring @Aspect实现切面编程: XML: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springf

Spring项目的配置文件们(web.xml context servlet springmvc)

我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp页面的web项目,你也必须配置这个文件.我们的java web项目肯定写了很多servlet代码,这些servlet需要运行在servlet容器中,这个容器就是tomcat的重要组件.也就是,你的web项目需要运行在tomcat中,那么你必须提供一个web.xml文件作为配置文件.在这个文件中,通过

Spring进阶之路(11)-使用Aspectj切面配置和XML配置文件方式实现切面编程

异常 在使用的时候,遇到了部分的异常,我用的是最新的Spring版本,Spring-4.2.5版本的,首先确保你的配置文件中引入了下面红色部分. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <span style="color:#ff0000;">

【Spring】AOP之基于XML配置总结与案例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.AOP的一些概念 AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封装.继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合.当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力.也就是说,OOP允许你定义从上到下的关系,但

Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一个简单的实现四则运算的计算器. 加入AOP功能:日志功能:检测参数中是否有负数的功能. 废话不多说了,直接上代码: (一)基于XML配置: 定义了一个接口类: package com.edu.aop; public interface ArithmeticCalculator { int add(i