WEB开发中SpringAOP实际应用一例_第1页
WEB开发中SpringAOP实际应用一例_第2页
WEB开发中SpringAOP实际应用一例_第3页
WEB开发中SpringAOP实际应用一例_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、最新资料推荐WEB开发中Spring AOP实际应用一例WEB开发中Spring AOP实际应用一例 在WEB开发中,用户对网页的访问权限检查是一个重要的环节。以STRUST为例,我们需要在Action 的excute方法中编写相 关的代码(一般是调用基类的函数),也很显然,在每个Action中 这是一种重复劳动。如果我们在excute运行之前,能够自动去调用基类的权限检 查函数,这无疑是个好的解决办法。AOP就为我们提供了这样一种解决方法。下面以一个简化的实例介绍实现的办法。首先我们做一个接口:public in terface Check In terface public abstrac

2、t voidcheck(Stringname) ; public abstract void excute(Stringname) ; 再做一个基类:publicabstractclass BaseClass impleme ntsCheck In terface public BaseClass() public void check(Str ing n ame) if (n ame. equals(supervisor) ) System.out. println( Check Pass!) ; else System. out. println(No access privilege!

3、Please do sth. else!) ; 再做一个测试类:public class ExcuteClass extends BaseClass publicout. prin tl n( Excute here!+name) ; 好了, 下面做一个通知类(Advice ):import org. spri ngframework. aop. MethodBeforeAdvice;import java. lang. reflect. Method; import org. apache. log4j. Logger;public class BeforeAdvisor impleme n

4、tsMethodBeforeAdvice private static Logger logger=Logger. getLogger(BeforeAdvisor. class) ; public void before(Method m, Object args, Object target) throws Throwable if(targetin sta nceofCheckI nterface) logger. debug(IsIn sta nceofCheckI nterface!);Check In terfaceci=(Checkl nterface)target;ci.chec

5、k(Stri ng)args0) ; 其中重要的before 方法的参数:Object target 传入的通知的对象(即测试类的接口),Methodm, Object args分别是该对象被调用的方法和参数。我们再来作spring bean 定义xml文件:?xml versio n=1. 0 en codi ng二UTF-8? !DOCT YPE bea ns PUBLIC-/SPRING/DTD BEAN/EN . wysm. netstar. test. springaop. BeforeAdvisor/bea n id二myPo in tcutAdvisor2class=org.sp

6、ri ngframework. aop. support. RegexpMethodPo in tcutAdvisor property name=advice ref local=MyAdvisor / /property property n ame=patter ns list value. *excute. */value /list /property最新资料推荐/bea n bea n id二check In terfaceclass二com. wysm. n etstar. test.springaop. ExcuteClass/ bean id二myCheckClass cla

7、ss=org. springframework. aop. framework. ProxyFactoryBean property n ame二proxy In terfaces valuecom. wysm. n etstar. test. spri ngaop. Check In terface/value/property propertyn ame=targetreflocal=check In terface/propertypropertyn ame=in terceptorNamesvaluemyPo in tcutAdvisor2/value/property /bea n

8、/bea ns这个定义文件指明了 ExcuteClass 为监视对象,它的excute方法被执行的时候,BeforeAdvisor 将被调用。最后是测试类:importjun it. framework. TestCase;import org.spr in gframework.con text.Applicati onCon text;import org.spri ngframework.con text.support.FileSystemXmlApplicatio nCon text;publicclassSprin gTestCase2 exte nds TestCase CheckI nterface test 二n ull;ctx二newprotected void setUp() throws Exception super. setUp();Applicatio nCon textFileSystemXmlApplicatio nCon text(src/com/wysm/netstar/test/sprin gaop/aoptest. xml) ; test = (Check In terface)ctx.g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论