`
BradyZhu
  • 浏览: 248335 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring In Action读书笔记之四------------------AOP初步

 
阅读更多

Spring提供的AOP(Aspect-oriented Program)面向切面编程,是Spring的非常强大的功能。他能提供对方法的拦

截,然后在执行方法之前或者执行方法之后,甚至对执行方法的返回值异常等等情况执行相对应的代码,使得类似日

志,安全,权限拦截等功能都与逻辑代码分离,使代码专注于自己的任务。

下面我们来演示一个小例子,来说明一下使用Spring来进行AOP编程的过程。

首先写一个测试类

package com.bird.springidol;

public class Audience {
	
	public void takeSeats(){
		System.out.println("The audience is taking their seats");
	}
	
	public void turnOffCellPhones(){
		System.out.println("The audience is turning off their cellphone");
	}
	
	public void applaud(){
		System.out.println("clap clap clasp");
	}
	
	public void demandRefund(){
		System.out.println("Boo! We want our money back!");
	}
}

这个类将是一会演示AOP的演示效果的类。

然后就是需要拦截的方法,这里同样,我们写一个类

package com.bird.springidol;

public class Perform {
	
	public void perform(){
		System.out.println("Now Let's Perform");
	}
}

然后分别将他们配置到Spring容器中进行管理

<bean id="audience" class="com.bird.springidol.Audience"/>
   <bean id="perform" class="com.bird.springidol.Perform"/>

下面就是在XML中声明aspect了。首先需要在XML头引入AOP约定

<?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-3.0.xsd
	  http://www.springframework.org/schema/aop 
	  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

然后开启aspectj

  <aop:aspectj-autoproxy/>

下面就可以进行配置了。

 <aop:config>
   	<aop:aspect ref="audience">
   		<aop:before pointcut="execution(* com.bird.springidol.Perform.perform(..))"
   		method="takeSeats"/>
   	</aop:aspect>
   </aop:config>

首先来解释一下
execution(* com.bird.springidol.Perform.perform(..))
这个是apsectj的表达式,其中*代表不关心这个方法的返回值,里面的就是需要拦截的方法的完整名称,括号里面的..代表不关心
这个方法的参数。

这样就会在运行perform的方法的时候,提前运行takeSeat这个方法。

如果对一个方法进行多次拦截,这样写就会变的很麻烦,我们来演示一下一种简单的配置方法    
 <aop:config>
    	<aop:aspect ref="audience">
    		<aop:pointcut expression="execution(* com.bird.springidol.Perform.perform(..))" 
    		id="performance"/>
    		
    		<aop:before method="takeSeats" pointcut-ref="performance"/>
    		<aop:before method="turnOffCellPhones" pointcut-ref="performance"/>
    		<aop:after-returning method="applaud" pointcut-ref="performance"/>
    		<aop:after-throwing method="demandRefund" pointcut-ref="performance"/>
    	</aop:aspect>
    </aop:config>

这样给aop的切点表示了一个运行表达式并且给予了唯一的id,然后下面的切点就可以直接引用这个id来进行切面编程了。

这种方法在进行大范围拦截的时候很有用

分享到:
评论

相关推荐

    J2EE框架_笔记_c

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    J2EE框架_笔记_b

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    J2EE三大框架_笔记_a

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    javaEE框架笔记,识货人下

    48Spring_AOP笔记.pdf 49-Spring_JDBC模板笔记.pdf 50-Spring_Struts、Hibernate支持笔记.pdf 51-52使用Struts + Spring + Hibernate完成用户登陆笔记.pdf 53使用Struts + Spring + Hibernate完成分页笔记.pdf 54...

    xmljava系统源码-SpringInAction4:《SpringInAction4th》学习笔记

    SpringInAction4 《Spring In Action 4th》学习笔记 第一部分 Spring的核心 1. Spring之旅 依赖注入 AOP bean的初始化过程 spring容器 2. 装配Bean “initialization on demand holder”创建单例模式的理解,参考 ...

    spring项目开发学习笔记

    Spring是一个轻量级的DI/IoC和AOP容器框架。存在的目的是用于构建轻量级的J2EE应用。 轻量级:应用大小和应用开支,包括应用方式 依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不...

    springmybatis

    mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action...

    Java学习笔记-个人整理的

    {5.2.1}将浮点数四舍五入到指定精度}{98}{subsection.5.2.1} {6}Exception}{99}{chapter.6} {6.1}\ttfamily try-catch}{99}{section.6.1} {6.2}\ttfamily finally}{100}{section.6.2} {6.3}\ttfamily throws}{...

    JBoss ESB 学习笔记

    自己辛苦整理的网上的...13——第十二个ESB代码Spring AOP 113 14——第十三个ESB代码Transform CSV to XML 122 15——第十四个ESB代码Transform XML to POJO 128 16——第十五个ESB代码Web Service Consumer 1 151

    spring学习笔记

    Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................

    Java/JavaEE 学习笔记

    第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 ...

    J2EE学习笔记(J2ee初学者必备手册)

    第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 PL...

Global site tag (gtag.js) - Google Analytics