`
xuxiaolei
  • 浏览: 148812 次
  • 性别: Icon_minigender_1
  • 来自: 彩虹之巅
社区版块
存档分类

spring中AOP代理的几种方式

阅读更多

部分例子摘自 spring in action

(1)使用ProxyFactoryBean的代理

package chapter4;

public interface Performable {
	public void perform() throws Exception;
}

package chapter4;

import java.util.Random;

public class Artist implements Performable {

	public void perform() throws Exception {
		int num = new Random().nextInt(100);
		
		if(num >= 50) {
			throw new Exception(String.valueOf(num));
		} else {
			System.out.println(num);
		}
	}
}

package chapter4;

public class Audience {
	public 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 cellphones");
	}

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

	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
}

package chapter4;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

public class AudienceAdvice 
	implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {
	
	private Audience audience;
	
	public void setAudience(Audience audience) {
		this.audience = audience;
	}

	public void before(Method method, Object[] args, Object target)
		throws Throwable {
		audience.takeSeats();
		audience.turnOffCellPhones();
	}

	public void afterReturning(Object returnValue, Method method, Object[] args,
			Object target) throws Throwable {
		audience.applaud();
	}
	
	public void afterThrowing(Exception ex) {
		audience.demandRefund();
	}
}
 
<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<bean id="audience" class="chapter4.Audience" />
	
	<bean id="audienceAdvice" class="chapter4.AudienceAdvice" >
		<property name="audience" ref="audience" />
	</bean>
	
	<bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
		<property name="advice" ref="audienceAdvice" />
		<property name="expression" value="execution(* *.perform(..))" /> 
	</bean>
	
	<bean id="artistTarget" class="chapter4.Artist" />
	
	<bean id="artist" class="org.springframework.aop.framework.ProxyFactoryBean" >
		<property name="target" ref="artistTarget" />
		<property name="interceptorNames" value="audienceAdvisor" />
		<property name="proxyInterfaces" value="chapter4.Performable" />
	</bean>
</beans>

 (2)隐式使用ProxyFactoryBean的aop代理

DefaultAdvisorAutoProxyCreator实现了BeanPostProcessor,它将自动检查advisor的pointcut是否匹配bean的方法,如果匹配会替换bean为一个proxy,并且应用其advice。

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
	
	<bean id="audience" class="chapter4.Audience" />
	
	<bean id="audienceAdvice" class="chapter4.AudienceAdvice" >
		<property name="audience" ref="audience" />
	</bean>
	
	<bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
		<property name="advice" ref="audienceAdvice" />
		<property name="expression" value="execution(* *.perform(..))" /> 
	</bean>
	
	<bean id="artist" class="chapter4.Artist" />
</beans>

 (3)使用注解的aop代理

xml中增加了一个<aop:aspectj-autoproxy />,它创建了AnnotationAwareAspectJAutoProxyCreator在spring中,这个类将自动代理匹配的类的放方法。和上个例子中DefaultAdvisorAutoProxyCreator做同样的工作。

package chapter4;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Audience {
	public Audience() {
	}
	
	@Pointcut("execution(* *.perform(..))")
	public void pointcut(){}

	@Before("pointcut()")
	public void takeSeats() {
		System.out.println("The audience is taking their seats.");
	}

	@Before("pointcut()")
	public void turnOffCellPhones() {
		System.out.println("The audience is turning off " + "their cellphones");
	}
	@AfterReturning("pointcut()")
	public void applaud() {
		System.out.println("CLAP CLAP CLAP CLAP CLAP");
	}
	@AfterThrowing("pointcut()")
	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
}
 
<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<aop:aspectj-autoproxy />
	
	<bean id="audience" class="chapter4.Audience" />

	<bean id="artist" class="chapter4.Artist" />
</beans>

 (4)使用aop配置文件的自动代理

采用这种方法,不用加<aop:aspectj-autoproxy />

package chapter4;

import org.aspectj.lang.annotation.Aspect;

@Aspect
public class Audience {
	public Audience() {
	}

	public void pointcut() {
	}

	public void takeSeats() {
		System.out.println("The audience is taking their seats.");
	}

	public void turnOffCellPhones() {
		System.out.println("The audience is turning off " + "their cellphones");
	}

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

	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
}
 
<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<bean id="audience" class="chapter4.Audience" />
	
	<aop:config>
		<aop:aspect ref="audience">
			<aop:before method="takeSeats" pointcut="execution(* *.perform(..))" />
			<aop:before method="turnOffCellPhones" pointcut="execution(* *.perform(..))" />
			<aop:after-returning method="applaud" pointcut="execution(* *.perform(..))" />
			<aop:after-throwing method="demandRefund" pointcut="execution(* *.perform(..))" />
		</aop:aspect>
	</aop:config>

	<bean id="artist" class="chapter4.Artist" />
	
</beans>
分享到:
评论

相关推荐

    Spring aop 之 静态代理 动态代理 Aspectj aop-config 等实现方式

    主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...

    Spring AOP源码分析.mmap

    有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系... 确实,Spring也就是通过这两种方式来实现AOP相关功能,下面就通过源码来简单求证下

    spring事务管理几种方式代码实例

    spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...

    吴天雄--Spring笔记.doc

    IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含单例设计模式),DI详解,Spring的几种注入方式,利用Spring简化Mybatis;第二天内容:AOP(AOP常用概念、Spring的三种aop实现方式、代理...

    SpringAOP使用介绍,从前世到今生

    SpringAOP发展到现在出现的全部3种配置方式。由于Spring强大的向后兼容性,实际代码中往往会出现很多配置混杂的情况,而且居然还能工作,本文希望帮助大家理清楚这些知识。我们先来把它们的概念和关系说说清楚。AOP...

    高级开发spring面试题和答案.pdf

    传播特性有几种?7种; 某一个事务嵌套另一个事务的时候怎么办? REQUIRED_NEW和REQUIRED区别 Spring的事务是如何回滚的,实现原理; 抽象类和接口的区别,什么时候用抽象类什么时候用接口; StringBuilder和...

    SpringFramework常见知识点.md

    - Spring依赖注入的方式有几种? - 一个bean的定义包含了什么?(BeanDefinition) - bean的作用域有哪些? - Spring 的扩展点主要有哪些? - Spring如何解决循环依赖? - 事务的传播行为是什么?有哪些? - 什么是AOP...

    Spring面试题含答案.pdf

    25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...

    开源框架 Spring Gossip

    从代理机制初探 AOP 动态代理 &lt;br&gt;AOP 观念与术语 Spring AOP Advices Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。 Before ...

    Spring in Action(第二版 中文高清版).part2

    4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...

    Spring in Action(第二版 中文高清版).part1

    4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1 Spring的数据访问哲学 5.1.1 了解Spring数据...

    Spring.net框架

    本部分代码仅仅提供一种功能演示,如果实际应用仍需进一步完善(建议使用一些成型的Ioc框架,例如Spring.net或Castle等)。经过改造后 的系统,组件间依赖关系如下图: 可以看出这次实现了真正的“针对接口编程”...

    利用Java的反射与代理实现IOC模式

    而代理是一种基本的设计模式,它是一种为了提供额外的或不同的操作而插入到真 实对象中的某个对象。而Java的动态代理在代理上更进一步,既能动态的创建代理对象,又能动态的调用代理 方法。Java的反射和动态代理机制...

    Spring in Action(第2版)中文版

    16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring a.1.1研究spring发布 a.1.2构建自己的类路径 a.2把spring添加为一...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    java面试题

    多线程几种实现方法,同步? 答:多线程有两种实现方法,一种是继承Thread类或者实现Runnable接口。同步就是在方法返回类型后面加上synchronized。 c#中的委托,事件是不是委托? 答:委托就是将方法作为一个参数...

    Java常见面试题208道.docx

    15.java 中 IO 流分为几种? 16.BIO、NIO、AIO 有什么区别? 17.Files的常用方法都有哪些? 二、容器 18.java 容器都有哪些? 19.Collection 和 Collections 有什么区别? 20.List、Set、Map 之间的区别是什么? 21....

    史上最全java面试,103项重点知识,带目录

    15. java 中 IO 流分为几种? 7 16. BIO、NIO、AIO 有什么区别? 7 17. Files的常用方法都有哪些? 8 二、容器 8 18. java 容器都有哪些? 8 19. Collection 和 Collections 有什么区别? 9 20. List、Set、Map 之间...

    阿里巴巴,天猫,支付宝面试题

    14. spring的bean配置的几种方式 15. web.xml的配置 16. spring的监听器。 17. zookeeper的实现机制,有缓存,如何存储注册服务的 18. IO会阻塞吗?readLine是不是阻塞的 19. 用过spring的线程池还是java的线程池? ...

    Java 基础核心总结 +经典算法大全.rar

    关于 null 的几种处理方式大小写敏感 null 是任何引用类型的初始值 null 只是-种特殊的值使用 Null-Safe 方法null 判断 关于思维导图 Java.IO Java.lang Java.math Java.net Java 基础核心总结 V2.0 IO 传统的 ...

Global site tag (gtag.js) - Google Analytics