前面学习了如何用注解的方式去配置Spring aop,今天把XML配置的方法也看了下,下面顺便也做了个记录

先把spring中用xml配置aop的配置元素给贴出来:

  • <aop:advisor> 定义AOP通知器
  • <aop:after> 定义AOP后置通知(不管被通知的方法是否执行成功)
  • <aop:after-returning> 定义AOP返回通知
  • <aop:after-throwing> 定义AOP异常通知
  • <aop:around> 定义AOP环绕通知
  • <aop:aspect> 定义一个切面
  • <aop:aspectj-autoproxy> 启用 @AspectJ 注解驱动的切面
  • <aop:before> 定义一个AOP前置通知
  • <aop:config> 顶层的AOP配置元素。大多数的 <aop:*> 元素必须包含在 <aop:config> 元素内
  • <aop:declare-parents> 以透明的方式为被通知的对象引入额外的接口
  • <aop:pointcut> 定义一个切点

还是先创一个接口类:

public interface Song {
void song();
}

接口实现类:

public class ManMan implements Song {

    public void song() {
System.out.println("下面这首歌是张学友的《慢慢》");
}
}

下面就是需需要定义的一个类,写通知的时候需要调用的方法

public class VocalConcert {

    public void checking() {
System.out.println("检票之后,找位子坐下");
} public void beautiful() {
System.out.println("演唱会进入精彩部分的时候,鼓掌!");
} public void leave() {
System.out.println("结束后,我们离开场地");
} public void watchVocalConcert(ProceedingJoinPoint joinPoint) {
try {
System.out.println("检票之后,找位子坐下");
joinPoint.proceed();
System.out.println("演唱会进入精彩部分的时候,鼓掌!");
System.out.println("结束后,我们离开场地");
} catch (Throwable throwable) {
System.out.println("效果不好,退票");
} }
}

好了,上面有已经有一个实现类,我们要让他可以实现自动注入,同时通知的类也要注入

然后就要配置 配置的切片什么时候通知消息了:

<?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"> <bean id="vocalConcert" class="com.aop.test2.VocalConcert" /> <bean class="com.aop.test2.ManMan" id="manMan"/> <!--一般的前后通知消息 -->
<aop:config>
<aop:aspect ref="vocalConcert">
<!-- vocalConcert 实现类调用之前,实现的方法 -->
<aop:before pointcut="execution(* com.aop.test2.Song.song(..))" method="checking"/>
<!-- vocalConcert 实现类调用之后,实现的方法 -->
<aop:after pointcut="execution(* com.aop.test2.Song.song(..))" method="beautiful"/>
<!-- vocalConcert 实现类调用结束,返回的时候,实现的方法 -->
<aop:after-returning pointcut="execution(* com.aop.test2.Song.song(..))" method="leave"/> <!-- 同一个切片,定一个id 调用的方法-->
<!-- <aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(..))"/>
<aop:before pointcut-ref="song" method="checking"/>
<aop:after pointcut-ref="song" method="beautiful"/>
<aop:after-returning pointcut-ref="song" method="leave"/>-->
</aop:aspect>
</aop:config> <!--环绕通知 -->
<aop:config>
<aop:aspect ref="vocalConcert">
<aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(..))"/>
<aop:around method="watchVocalConcert" pointcut-ref="song"/>
</aop:aspect>
</aop:config> </beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("config.xml")
public class VocalConcertTest { @Autowired
Song mm; @Test
public void log() {
mm.song();
} }

这就是简单的XML配置spring aop的方法,当然还有,还有带参数的xml配置,其主要定义的切点的时候不同,假设song()的方法是要传入参数的,参数类型为String,那么配置应该如下:

 <aop:config>
<aop:aspect ref="vocalConcert">
<aop:pointcut id="song" expression="execution(* com.aop.test2.Song.song(String) )and args(song)" />
</aop:aspect>
</aop:config>

主要就是 expression="execution(* com.aop.test2.Song.song(String) )and args(song)" 这边需要更改,跟前面用注解配置没有多大的区别

当然这个时候所对应的bean也要带参数,这个前面就学过,就不记录了。

代码:https://github.com/eoooxy/springinaction test下 的com.aop.test2 中

SpringInAction--XML配置Spring Aop的更多相关文章

  1. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

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

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

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

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

  4. 关于xml配置实现AOP的小知识

    除了前面介绍的基于JDK1.5的注解方式来定义切面,切入点和增强处理外,Spring AOP也允许直接使用XML配置文件来管理它们.在JDK1.5之前,只能使用配置文件的方式来管理,在Spring2. ...

  5. 基于@AspectJ配置Spring AOP之一--转

    原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...

  6. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  7. 怎样配置spring aop

    1.spring aop配置如下: 1.aspect切面是一个具体类,里面包含各种执行的通知方法.切面类也要注册到ioc容器中. 2.切入点pointcut,可以在每个通知里单独配置,即每个通知可以指 ...

  8. XML配置spring session jdbc实现session共享

    概述 session的基础知识就不再多说. 通常,我们会把一个项目部署到多个tomcat上,通过nginx进行负载均衡,提高系统的并发性.此时,就会存在一个问题.假如用户第一次访问tomcat1,并登 ...

  9. Spring XML配置实现AOP

    1:  首先我们要定义 配置成切面的类 package cn.gbx.example; import org.aspectj.lang.ProceedingJoinPoint; import org. ...

随机推荐

  1. JS 中根据iframe子页面自动iframe高度

    注意:为使页面有更好的兼容性,在使用以下代码前,请将aspx页中头部的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...

  2. 20145303 实验一 Java开发环境的熟悉(Linux + Eclipse)

    20145303 实验一 Java开发环境的熟悉(Linux + Eclipse) 实验题目(4):实现学生成绩管理功能,并进行测试 思路: 对于实现学生成绩管理(student performanc ...

  3. 20145307陈俊达《信息安全系统设计基础》第5周学习总结PT1

    20145307陈俊达<信息安全系统设计基础>第5周学习总结 教材学习内容总结 X86寻址方式经历三代: DOS时代的平坦模式,不安全,原因是没有区分用户空间和内核空间 8086的分段模式 ...

  4. 20145310 《Java程序设计》第1周学习总结

    20145310 <Java程序设计>第1周学习总结 教材学习内容总结 第一周主要学习教材前两章的知识.第一章主要学习了java的历史,版本的迁移以及一些相关的专有名词之间的联系和下载安装 ...

  5. 20145312 《Java程序设计》第五周学习总结

    20145312 <Java程序设计>第五周学习总结 学习笔记 Chapter8 异常处理 8.1语法与继承架构 1.Java中的错误以对象方式呈现,只要捕捉包装错误的对象,就可以针对该错 ...

  6. char,short,int长度

    数据类型的本质就是固定内存大小的别名 char:1byte short:  2byte int:4byte 其实变量也是对连续内存的别名,相当于这段内存的句柄.钩子

  7. SaltStack日常维护-第七篇

    练习内容 远程执行其他模块 官方模块有很多超过300+ 1.cmd.run 2.network 3.service 4.state 5.其它日常维护 演示 cmd.run模块 可以执行系统命令,超级模 ...

  8. Python学习札记(三) I/O

    参考:输入和输出 I/O 1.print()函数 a.调用print()输出字符串有以下两种方式:(1)print('[字符串]') (2)print("[字符串]") b.调用p ...

  9. java高级特性(4)--枚举

    枚举(enum),是指一个经过排序的.被打包成一个单一实体的项列表.一个枚举的实例可以使用枚举项列表中任意单一项的值.枚举在各个语言当中都有着广泛的应用,通常用来表示诸如颜色.方式.类别.状态等等数目 ...

  10. 简单的使用hibernate插入数据的例子

    数据库创建脚本: drop table person create table person( id          varchar(32)         not null primary key ...