`
javafenger
  • 浏览: 243262 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

WEB开发中Spring AOP实际应用一例 --有关STRUTS框架

阅读更多
         在WEB开发中,用户对网页的访问权限检查是一个重要的环节。以STRUST为例,我们需要在Action的excute方法中编写相关的代码(一般是调用基类的函数),也很显然,在每个Action中这是一种重复劳动。
如果我们在excute运行之前,能够自动去调用基类的权限检查函数,这无疑是个好的解决办法。AOP就为我们提供了这样一种解决方法。

  下面以一个简化的实例介绍实现的办法。

  首先我们做一个接口:
 

public interface CheckInterface {
  public abstract void check(String name);
  public abstract void excute(String name);
}



  好了,下面做一个通知类(Advice):

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;

public class BeforeAdvisor implements MethodBeforeAdvice {
private static Logger logger=Logger.getLogger(BeforeAdvisor.class);
public void before(Method m, Object[] args, Object target) throws Throwable {
if (target instanceof CheckInterface){
logger.debug("Is Instanceof CheckInterface!!!");
CheckInterface ci=(CheckInterface)target;
ci.check((String)args[0]);
}
}
}

  其中重要的before方法的参数:Object target传入的通知的对象(即测试类的接口),Method m, Object[] args分别是该对象被调用的方法和参数。我们再来作spring bean定义xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<description>Spring Quick Start</description>
<bean id="MyAdvisor" class="com.wysm.netstar.test.springaop.BeforeAdvisor"/>

<bean id="myPointcutAdvisor2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="MyAdvisor" />
</property>
<property name="patterns">
<list>
<value>.*excute.*</value>
</list>
</property>
</bean>

<bean id="checkInterface" class="com.wysm.netstar.test.springaop.ExcuteClass"/>

<bean id="myCheckClass" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.wysm.netstar.test.springaop.CheckInterface</value>
</property>
<property name="target">
<ref local="checkInterface" />
</property>
<property name="interceptorNames">
<value>myPointcutAdvisor2</value>
</property>
</bean>

</beans>

  这个定义文件指明了ExcuteClass为监视对象,它的excute方法被执行的时候,BeforeAdvisor将被调用。

  最后是测试类:

import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringTestCase2 extends TestCase {
CheckInterface test=null;

    protected void setUp() throws Exception {
super.setUp();
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/com/wysm/netstar/test/springaop/aoptest.xml");
test = (CheckInterface) ctx.getBean("myCheckClass");
}

    protected void tearDown() throws Exception {
super.tearDown();
}
public void testExcute(){
test.excute("supervisor");
}
}

  再做一个基类:

public abstract class BaseClass implements CheckInterface {
public BaseClass() {
}
public void check(String name){
if (name.equals("supervisor"))
System.out.println("Check Pass!!");
else {
System.out.println("No access privilege! Please do sth. else!");
}
}
}

  再做一个测试类:

public class ExcuteClass extends BaseClass {
public ExcuteClass() {
}

public void excute(String name){
System.out.println("Excute here!"+name);
}
}

分享到:
评论
1 楼 pujia12345 2009-08-25  
大哥 拦截器不就行了!

相关推荐

    Spring5 框架 ---- AOP ---- 代码

    Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring.jar spring-agent.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar

    spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-4.3.20.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar; 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar; 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

    Spring Aop的底层实现技术 --- Jdk动态代理原理

    Spring Aop的底层实现技术 --- Jdk动态代理原理 很不错的一篇文章

    AOP流程源码分析-SpringAOP中定义的类图

    AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-...

    spring-aop.jar

    spring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jar

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    spring-aop-2.0.8.jar

    spring-aop-2.0.8.jar

    spring-aop-5.2.7.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.2.7.RELEASE.jar; 赠送原API文档:spring-aop-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.7.RELEASE.pom;...

    spring-aop-5.0.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.5.RELEASE.jar; 赠送原API文档:spring-aop-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.5.RELEASE.pom;...

Global site tag (gtag.js) - Google Analytics