spring aop的两种实现方式(续1)

接上次。。。
第一种实现方式:针对于拦截多个包中的某一规则的方法
[b]配置文件:[/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: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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="false" default-dependency-check="none" default-autowire="no">
<bean name="advice" class="cn.icbc.service.bs.impl.AopAdvice"/>
<aop:config>
<aop:aspect id="myAspect" ref="advice">
<aop:before pointcut-ref="rmiService" method="before"/>
<aop:after pointcut-ref="rmiService" method="after"/>
<aop:around pointcut-ref="rmiService" method="around "/> </aop:aspect>
</aop:config>
<aop:config>
<aop:pointcut id="rmiService" expression="execution(* cn.icbc.service.bs.impl.*.*(..))" />
</aop:config>

关键拦截类AopAdvice.java:
package cn.icbc.service.bs.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class AopAdvice
{

public void before() throws Throwable {

System.out.println("spring aop before 权限拦截测试....");
}

public void after() throws Throwable {

System.out.println("spring aop after 权限拦截测试....");
}


public Object around(ProceedingJoinPoint p) throws Throwable {

System.out.println("spring aop around before 权限拦截测试....");
Object o=p.proceed();
System.out.println("spring aop around after 权限拦截测试....");
return o;

}



}


第二种实现方式:拦截某个类下的所有方法
配置文件:
<?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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" default-lazy-init="false" default-dependency-check="none" default-autowire="no">
<bean name="advisor" class="cn.icbc.service.bs.impl.AopSingleAdvice" />
<bean id="addProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--属性proxyInterface定义了接口类 -->
<property name="proxyInterfaces" value="cn.icbc.service.bs.iface.IService"></property>
<!-- 属性target指向本地配置的一个bean,这个bean返回一个接口的实现 -->
<property name="target" ref="ServiceImpl"></property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>

</property>

</bean>

拦截类AopSingleAdvice.java实现

package cn.icbc.service.bs.impl;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class AopSingleAdvice implements MethodBeforeAdvice, AfterReturningAdvice,MethodInterceptor
{

public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("spring aop afterReturning 权限拦截测试....");
}

public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("spring aop before 权限拦截测试....");

}

public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("spring aop arount before 权限拦截测试....");
Object o=arg0.proceed();
System.out.println("spring aop arount after 权限拦截测试....");
return o;
}





}

补充说明:
1、两种实现方式的应用场景并不是绝对的,相互间可以通用,对于第二种方式中,可以引入org.springframework.aop.support.NameMatchMethodPointcutAdvisor和
org.springframework.aop.support.RegexpMethodPointcutAdvisor 来应用于跨包的拦截(待下次说明具体配置和实现)
2、例子只是列举了before,after,around的通知方式,其它的于此相似,可以自行举一反三.
3、此两种方式是在spring2.0的基础上实现的
4、第二种方式下,获得bean的名字应该是addProxy。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值