AOP实现方法
原文地址
http://michael-softtech.iteye.com/blog/650779
(1)使用ProxyFactoryBean的代理
Java代码
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代码
<?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.AspectJExpressionPointcu
tAdvisor">
<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代码
<?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.DefaultAdvisorAutoProxyC
reator" />
<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.AspectJExpressionPointcu
tAdvisor">
<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做同样的工作。
Java代码
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代码
<?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 />
Java代码
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代码
<?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>
AOP实现方法的更多相关文章
- JS实现AOP拦截方法调用
//JS实现AOP拦截方法调用function jsAOP(obj,handlers) { if(typeof obj == 'function'){ obj = obj.prot ...
- AOP记录方法的执行时间
作用AOP监控方法的运行时间如下: @Component @Aspect public class LogAop { private Logger log = LoggerFactory.getLog ...
- 动态代理AOP实现方法过滤
上一节实现了动态代理,接下来 有时候,我不需要在每一个方法都要记录日志,做权限验证 等等. 所有就有了这样的需求.AOP实现特定方法过滤,有选择性的来对方法实现AOP 拦截.就是本节标题所示. 举个例 ...
- AOP获取方法注解实现动态切换数据源
AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...
- 使用Spring Aop验证方法参数是否合法
先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.ann; import java.lang.annot ...
- 使用AOP实现方法执行时间和自定义注解
环境:IDEA2018+JDK1.8+SpringBoot 第一步:在pom文件中引入依赖(度娘有很多(*^▽^*)): <!--引入AOP的依赖--><dependency> ...
- (一)七种AOP实现方法
在这里列表了我想到的在你的应用程序中加入AOP支持的所有方法.这里最主要的焦点是拦截,因为一旦有了拦截其它的事情都是细节. Approach 方法 Advantages 优点 Disadvantage ...
- JAVA动态代理和方法拦截(使用CGLib实现AOP、方法拦截、委托)
AOP用CGLib更简便.更可控. 动态代理的实现非常优雅. 实体类: public class SampleClass { public String MyFunction1(String inpu ...
- AOP 增强方法
Spring AOP 提供了 5 种类型的通知,它们分别是 Before Advice(前置通知).After Returning Advice(后置通知).Interception Around A ...
随机推荐
- +load,+initialize原理
+load,+initialize原理 1.load 父类的load方法在子类load方法之前调用,分类的load方法在原来类load方法之后调用,依赖类的load方法会在自己之前调用,总之所有的类的 ...
- D2JS 的数据绑定
D2JS 将数据绑定视为"对象-路径-渲染/收集 "组成.主要 DOM 元素和对象绑定,称为 d2js.root,非主要元素指定数据路径,通过路径定位到值,根据值可进行渲染或收集 ...
- ASP.NET Boilerplate 工作单元
从上往下说起,框架使用castle拦截器,拦截实现了IApplication.IRepository接口的所有方法,和使用了UnitOfWork 特性的方法,代码如下 internal class U ...
- 使用Java BigDecimal进行精确运算
首先我们先来看如下代码示例: public class Test_1 { public static void main(String[] args) { System.out ...
- NetAdvantage webdatagrid 控件的一些属性
属性: 1 behaviors 行为下的属性集合 Row Selectors 主要用于设置行选择样式与形为的集合 Enable 属性表示是否启用 Row Selectors下的属性设置 RowNumB ...
- mysql locktables
SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, TIMESTAMPDIFF( ...
- mysql出现的错误
(一)ERROR 1005 (HY000): Can't create table '.\day19\user_role.frm' (errno: 121) 今天遇到的这个问题是因为创建了五张表,其中 ...
- Express在windows IIS上部署详解
最近公司在用Express+angularjs+wcf开发系统,让我在windows上部署系统,遇到不少问题,不过最后还是解决了,在IIS上部署系统, 首先windows需安装以下软件: 1.node ...
- 在模型中获取网络数据,刷新tableView
model .h #import <Foundation/Foundation.h> #import "AFHTTPRequestOperationManager.h" ...
- javascript基础学习(二)
javascript的数据类型 学习要点: typeof操作符 五种简单数据类型:Undefined.String.Number.Null.Boolean 引用数据类型:数组和对象 一.typeof操 ...