第三章 AOP 基于Schema的AOP

        基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已。

3.7.1一般增强的使用

a、目标类

public class Target {
	public void say(){
		System.out.println("say...");
	}

	public String getName(int id,String name){
		System.out.println("getName...");
		return "MR"+name+id;
	}

	public void around(){
		System.out.println("around...");
	}

	public void targetThrow(){
		System.out.println("targetThrow");
		throw new RuntimeException("我是一个运行期异常...");
	}
}

b、POJO(增强所在的类)

public class Pojo {
	public void before() {
		System.out.println("前置增强");
	}

	public void afterReturning(String retName, int id, String name) {
		System.out.println("后置增强,返回值为:"+retName+" 入参为:"+id+"-"+name);
	}

	public Object around(ProceedingJoinPoint point) throws Throwable{
		System.out.println("方法执行之前");
		Object object = point.proceed();
		System.out.println("方法执行之后");
		return object;
	}

	public void throwEx(Exception ex){
		System.out.println("抛出异常增强,异常信息:"+ex.getMessage());
	}

	public void finalEx(){
		System.out.println("Final增强");
	}
}

c、aop命名空间与Schema方式配置

<?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:p="http://www.springframework.org/schema/p"
	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-3.0.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 目标类 -->
	<bean id="target" class="cn.framelife.spring.schema.Target"></bean>

	<!-- 增强所在的类 -->
	<bean id="advisor" class="cn.framelife.spring.schema.Pojo"></bean>

	<!-- 配置基于schema的切面 -->
	<aop:config proxy-target-class="true">
		<!-- 确定增强所在类,并引入 -->
		<aop:aspect ref="advisor">
			<!—
                          前置增强
                          method是配置增强所在类中的方法
                        -->
			<aop:before method="before" pointcut="target(cn.framelife.spring.schema.Target)"/>

			<!—
                          后置增强
                          Returning 是返回值,必须和method中的参数名是一样的
                          在Schema配置中,多个切点函数的与操作是and,或操作是or
                        -->
			<aop:after-returning method="afterReturning" pointcut="execution(* cn.framelife.spring.schema..getName(..)) and args(id,name)" returning="retName" arg-names="retName,id,name"/>

			<!-- 环绕增强 -->
			<aop:around method="around" pointcut="execution(* cn.framelife.spring.schema..around(..))"/>

			<!—
                         抛出异常增强
                         throwing是异常对象,必须和method中的参数是一样的
                        -->
			<aop:after-throwing method="throwEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))" throwing="ex"/>

			<!-- Final增强 -->
			<aop:after method="finalEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))"/>
	</aop:config>
</beans>

d、测试

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		Target target = (Target) context.getBean("target");
		target.say();
		target.getName(10, "Zhang");
		target.around();
		target.targetThrow();

e、结果

前置增强
say...
前置增强
getName...
后置增强,返回值为:MRZhang10 入参为:10-Zhang
前置增强
方法执行之前
around...
方法执行之后
前置增强
targetThrow
抛出异常增强,异常信息:我是一个运行期异常...
Final增强
Exception in thread "main" java.lang.RuntimeException: 我是一个运行期异常...

3.7.2引介增强的使用

我们还是使用3.6.2@DeclareParents中的例子:Waiter为目标类,然后让目标类拥有ISeller接口的功能:

http://blog.csdn.net/p_3er/article/details/9269407

a、两个接口与两个类

目标类与其接口:

[java] view
plain
copyprint?

  1. public interface IWaiter {  
  2.     public void service();  
  3. }  

[java] view
plain
copyprint?

  1. @Component  
  2. public class Waiter implements IWaiter {  
  3.     @Override  
  4.     public void service() {  
  5.         System.out.println("service");  
  6.     }  
  7. }  

运行期织入到目标类的功能类与其接口:

[java] view
plain
copyprint?

  1. public interface ISeller {  
  2.     public void sell();  
  3. }  

[java] view
plain
copyprint?

  1. public class Seller implements ISeller {  
  2.     @Override  
  3.     public void sell() {  
  4.         System.out.println("sell");  
  5.     }  
  6. }  

b、配置

<?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:p="http://www.springframework.org/schema/p"
	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-3.0.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 目标类 -->
	<bean id="waiter" class="cn.framelife.spring.schema.Waiter"></bean>

	<!-- 增强所在的类 -->
	<bean id="advisor" class="cn.framelife.spring.schema.Pojo"></bean>

	<aop:config proxy-target-class="true">
		<!—
虽然引介增强不需要在增强所在的类中定义一个方法用于增强的实现,但<aop:aspect ref="advisor">中的ref属性依然要指定一个增强Bean
-->

		<aop:aspect ref="advisor">
			<!—
引介增强
	types-matching 目标类
	implement-interface 要织入目标类的接口
	default-impl 织入接口的实现类
-->
			<aop:declare-parents
				types-matching="cn.framelife.spring.schema.IWaiter+"
				implement-interface="cn.framelife.spring.schema.ISeller"
				default-impl="cn.framelife.spring.schema.Seller"/>
		</aop:aspect>
	</aop:config>
</beans>

c、测试

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
		IWaiter waiter = (IWaiter) context.getBean("waiter");
		waiter.service();
		ISeller seller = (ISeller)waiter;
		seller.sell();

d、结果

service
sell
时间: 2024-08-28 09:21:22

第三章 AOP 基于Schema的AOP的相关文章

spring学习笔记(13)基于Schema配置AOP详解

基于Schema配置入门实例 除了基于@AspectJ注解的形式来实现AOP外,我们还可以在IOC容器中配置.先来看看一个常见的应用场景,在我们的web项目中,我们需要为service层配置事务,传统的做法是在每个业务逻辑方法重复下面配置中: Created with Raphaël 2.1.0程序开始1. 获取DAO层封装好的数据库查询API,如HIbernate中的SessionFactory/Session和mybatis中的xxxMapper2. 开启事务3. 根据入参查询数据库完成相应

反精益创业第三章:基于公开资料的商业情报分析

摘要: 最了解你的人,往往是你的对手!古龙 4)目标公司的核心产品 我们除了在搜索引擎上搜索目标公司的信息之外,他的核心产品情况是一定不能遗漏的.仔细分析其产品在各个网页中的 最了解你的人,往往是你的对手!--古龙 4)目标公司的核心产品 我们除了在搜索引擎上搜索目标公司的信息之外,他的核心产品情况是一定不能遗漏的.仔细分析其产品在各个网页中的描述异同,会有很多有用的信息.比如,由于更新的时间不同,产品会有多个版本,那么观察不同版本出来之后,客户的评价和企业做的公关与造势,都会在网页中显现,归纳

我要造轮子之基于JDK的AOP实现

1 前言 Aspect Oriented Programing,面向切面编程. 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率. AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码. 像Spring

基于.Net的AOP实现技术

基于.Net的AOP实现技术 前言 在笔者的<面向对象的应用服务层设计>一文中,笔者讨论了软件系统中设计应用服务层所需要考虑的问题,以及系统分层的基本思考方法.这些讨论作关注的问题,都是系统中纵向的层次的划分.然而,在设计软件系统的时候,我们不仅仅要考虑纵向的关系,很多时候,我们还需要关注所谓的"横切关注点"的问题,例如,存在于系统每个部分的日志记录.安全性验证等.AOP(面向方面编程)的出现,便是为了解决这些"横切关注点"的问题. 虽然AOP目前还不是

Spring AOP基于配置文件的面向方法的切面

Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 after为方法后执行 这里只对around的方式进行介绍.本文只是摘录相应的思路,许多辅助类和方法不一一给出.因此下述方法并不能正常运行. 定义忽略权限检查注解类 @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.

Spring中的AOP(二)——AOP基本概念和Spring对AOP的支持

AOP的基本概念     AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤,希望以更好的方式来组合业务逻辑的各个步骤.AOP框架并不与特定的代码耦合,AOP框架能处理程序执行中特定切入点,而不与具体某个类耦合(即在不污染某个类的情况下,处理这个类相关的切点).下面是一些AOP的一些术语:     切面(Aspect):业务流程运行的某个特定步骤,也就是应用运行过程的关注点,关注点通常会横切多个对象,因此常被称为横切关注点     连接点(JoinPoi

什么是AOP系列之一:AOP概念解析

概念 为什么要区分J2EE容器和J2EE应用系统? 我们知道,J2EE应用系统只有部署在J2EE容器中才能运行,那么为什么划分为J2EE容器和J2EE应用系统? 通过对J2EE容器运行机制的分析,我们可以发现:实际上J2EE容器分离了一般应用系统的一些通用功能,例如事务机制.安全机制以及对象池或线程池等性能优化机制. 这些功能机制是每个应用系统几乎都需要的,因此可以从具体应用系统中分离出来,形成一个通用的框架平台,而且,这些功能机制的设计开发有一定难度,同时运行的稳定性和快速性都非常重要,必须经

&amp;gt; 前言(补充) 和第三章 第一个C#程序(rainbow 翻译)(来自重粒子空间)

程序 <<展现C#>> 前言(补充) 和第三章 第一个C#程序(rainbow 翻译)   出处:http://www.informit.com/matter/ser0000001/chapter1/ch03.shtml 正文: 前言0.1  提要    欢迎阅读<展现 C#>(Presenting C#).这本书是你提高企业编程语言的一条捷径.这种企业编程语言带有下一代编程语言服务运行时(NGWS Runtime):C#(发音"C sharp").

Google Web App开发指南第三章:案例研究

旅程计划应用(Wayfindit: Trip Planner App) 在大多数情况下,Wayfindit的应用必须有很好的易用性.旅行是一件很复杂的事情,不管是商业旅行还是休假旅行,一个顺利的旅程要求从家门到目的都没有意外之忧.Wayfindit的应用要能给旅行者提供所需信息,并且要快而准确.这意味着它需要一个最小的.直观的.响应式界面,能在前端提供有关内容的重要信息--HTML5的地理感知和离线存储特性实现. 一个完美的袖珍指南 它就装在你的口袋里或者包里,即时提供信息.它拥有本地存储和地理