`
63938525
  • 浏览: 46755 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多
接上次。。。
第一种实现方式:针对于拦截多个包中的某一规则的方法
配置文件:
<?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。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics