Spring中的AOP 应用

 

前些天小编曾经和大家探讨过AOP的概念,今天小编要和大家说说AOP的应用,希望对大家能够有所帮助。权限检查是AOP应用中一个热门话题,假设如果现在出现了一个设计完备的权限管理组件,那么将是一件多么惬意的事情,我们只需要在系统中配置一个AOP组件,即可完成以往需要大费周张才能完成的权限判定功能。可惜目前还没有这样一个很完善的实现。一方面权限检查过于复杂多变,不同的业务系统中的权限判定逻辑可能多种多样(如对于某些关键系统而言,很可能出现需要同时输入两个人的密码才能访问的需求)。另一方面,就目前的AOP应用粒度而言,“权限管理”作为一个切面尚显得过于庞大,需要进一步切分设计,设计复杂,实现难度较大。目前最为实用的AOP应用,可能就是Spring中基于AOP实现的事务管理机制,也正是这一点,使得Spring AOP大放异彩。

之前的内容中,我们大多围绕Spring AOP的实现原理进行探讨,这里我们围绕一个简

单的AOP Interceptor实例,看看SpringAOP机制的应用与开发。

在应用系统开发过程中,我们通常需要对系统的运行性能有所把握,特别是对于关键业

务逻辑的执行效能,而对于执行效能中的执行时间,则可能是重中之重。

我们这里的实例的实现目标,就是打印出目标Bean中方法的执行时间。

首先,围绕开篇中提到的几个重要概念,我们来看看Spring中对应的实现。

1. 切点(PointCut

一系列连接点的集合,它指明处理方式(Advice)将在何时被触发。

对于我们引用开发而言,“何时触发”的条件大多是面向Bean的方法进行制定。实

际上,只要我们在开发中用到了Spring的配置化事务管理,那么就已经进行了PointCut

设置,我们可以指定对所有save开头的方法进行基于AOP的事务管理:

<property name="transactionAttributes">

<props>

<prop key="save*">PROPAGATION_REQUIRED</prop>

</props>

</property>

同样,对于我们的AOP组件而言,我们也可以以方法名作为触发判定条件。

我们可以通过以下节点,为我们的组件设定触发条件。

<bean id="myPointcutAdvisor"

class="org.springframework.aop.support.RegexpMethodPointcutAdv

isor">

<property name="advice">

<ref local="MyInterceptor" />

</property>

<property name="patterns">

<list>

<value>.*do.*</value>

<value>.*execute.*</value>

</list>

</property>

</bean>

RegexpMethodPointcutAdvisorSpring中提供的,通过逻辑表达式指定方法

判定条件的支持类。其中的逻辑表达式解析采用了Apache ORO组件实现,关于逻

辑表达式的语法请参见Apache ORO文档。

上面我们针对MyInterceptor设定了一个基于方法名的触发条件,也就是说,当

目标类的指定方法运行时,MyInterceptor即被触发。

MyInterceptor是我们对应的AOP逻辑处理单元,也就是所谓的Advice

2Advice

Spring中提供了以下几种Advice:

1. Interception around advice

Spring中最基本的Advice类型,提供了针对PointCut的预处理、后处理过程

支持。

我们将使用Interception around advice完成这里的实例。

2. Before advice

仅面向了PointCut的预处理。

3. Throws advice

仅面向PointCut的后处理过程中的异常处理。

4. After Returning advice

仅面向PointCut返回后的后处理过程。

5. Introduction advice

Spring中较为特殊的一种Advice,仅面向Class层面(而不像上述Advice

向方法层面)。通过Introduction advice我们可以实现多线程访问中的类锁

定。

Spring中采用了AOP联盟(AOP Alliance12的通用AOP接口(接口定义位于

aopalliance.jar)。这里我们采用aopalliance.jar中定义的MethodInterceptor作为

我们的Advice实现接口:

public class MethodTimeCostInterceptor implements

MethodInterceptor,

Serializable {

protected static final Log logger = LogFactory.getLog(MethodTimeCostInterceptor.class);

public Object invoke(MethodInvocation invocation) throws

Throwable {

long time = System.currentTimeMillis();

Object rval = invocation.proceed();

time = System.currentTimeMillis() - time;

logger.info("Method Cost Time => " + time + " ms");

return rval;

}

}

对应配置如下:

<bean id="MyInterceptor"

class="net.xiaxin.interceptors.MethodTimeCostInterceptor"

/>

除此之外,我们还需要定义一个Spring AOP ProxyFactory用以加载执行AOP组件。

定义如下:

<bean id="myAOPProxy"

class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces">

<value>net.xiaxin.ITest</value>

</property>

<!是否强制使用CGLIB进行动态代理

<property name="proxyTargetClass">

<value>true</value>

</property>

-->

<property name="target">

<ref local="test" />

</property>

<property name="interceptorNames">

<value>myPointcutAdvisor</value>

</property>

</bean>

<bean id="test" class="net.xiaxin.Test"/>

其中的test是我们用于测试的一个类,它实现了ITest接口。

public interface ITest {

public abstract void doTest();

public abstract void executeTest();

}

public class Test implements ITest {

public void doTest(){

for (int i=0;i<10000;i++){}

}

public void executeTest(){

for (int i=0;i<25000;i++){}

}

}

通过以上工作,我们的MyInterceptor即被加载,并将在Test.doTest

Test.executeTest方法调用时被触发,打印出这两个方法的执行时间。

public void testAOP() {

ApplicationContext ctx=new

FileSystemXmlApplicationContext("bean.xml");

ITest test = (ITest) ctx.getBean("myAOPProxy");

test.doTest();

test.executeTest();

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值