WEB开发中Spring AOP实际应用一例
来源:互联网 发布:矩阵论 武汉大学 编辑:程序博客网 时间:2024/05/17 07:49
在WEB开发中,用户对网页的访问权限检查是一个重要的环节。以STRUST为例,我们需要在Action的excute方法中编写相关的代码(一般是调用基类的函数),也很显然,在每个Action中这是一种重复劳动。
如果我们在excute运行之前,能够自动去调用基类的权限检查函数,这无疑是个好的解决办法。AOP就为我们提供了这样一种解决方法。
下面以一个简化的实例介绍实现的办法。
首先我们做一个接口:
public interface CheckInterface {
public abstract void check(String name);
public abstract void excute(String name);
}
public abstract class BaseClass implements CheckInterface { 再做一个测试类: public class ExcuteClass extends BaseClass { public void excute(String name){ import org.springframework.aop.MethodBeforeAdvice; public class BeforeAdvisor implements MethodBeforeAdvice { 其中重要的before方法的参数:Object target传入的通知的对象(即测试类的接口),Method m, Object[] args分别是该对象被调用的方法和参数。我们再来作spring bean定义xml文件: <bean id="myPointcutAdvisor2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <bean id="checkInterface" class="com.wysm.netstar.test.springaop.ExcuteClass"/> <bean id="myCheckClass" class="org.springframework.aop.framework.ProxyFactoryBean"> </beans> 这个定义文件指明了ExcuteClass为监视对象,它的excute方法被执行的时候,BeforeAdvisor将被调用。 最后是测试类: import junit.framework.TestCase; public class SpringTestCase2 extends TestCase { protected void setUp() throws Exception { protected void tearDown() throws Exception { 文章来源:http://www.jiuyepx.cn/Html/jishu/java/3254.html
再做一个基类:
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 ExcuteClass() {
}
System.out.println("Excute here!"+name);
}
}
好了,下面做一个通知类(Advice):
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
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]);
}
}
}
<?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"/>
<property name="advice">
<ref local="MyAdvisor" />
</property>
<property name="patterns">
<list>
<value>.*excute.*</value>
</list>
</property>
</bean>
<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>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
CheckInterface test=null;
super.setUp();
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/com/wysm/netstar/test/springaop/aoptest.xml");
test = (CheckInterface) ctx.getBean("myCheckClass");
}
super.tearDown();
}
public void testExcute(){
test.excute("supervisor");
}
}
- WEB开发中Spring AOP实际应用一例
- WEB开发中Spring AOP实际应用一例
- WEB开发中Spring AOP实际应用一例
- Spring AOP实际应用一例
- Spring AOP实际应用一例
- Spring AOP实际应用一例
- Spring AOP实际应用一例
- Spring AOP实际开发应用
- Spring Aop应用一例
- Spring aop切面实际应用 XML配置
- Spring aop切面实际应用 XML配置
- Spring AOP实际项目中使用案例
- 使用Spring进行Web应用开发(一)
- aop实际应用
- AOP的实际应用
- Web开发中应用Spring框架
- web前端之dojo实际应用及开发一:开始dojo(附有源码)
- Spring中AOP开发步骤
- C# WinForm如何部署数据库
- MFC控件入门
- 收藏博文
- 海量数据库的查询优化及分页算法方案
- 关于环和域的18.3节问题的分析
- WEB开发中Spring AOP实际应用一例
- 水仙花数;穷举搜索;
- Cookie学习
- angle of polarized direction
- Google项目托管及VS2008的SVN插件AnkhSVN的使用
- Contact us for catalog and price list pls.
- 什么是程序集?
- 糟糕程序员的特征
- 向其它程序的TDataGrid发送数据的方法: