国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

WEB開發中SpringAOP實際應用一例

2019-11-18 13:47:20
字體:
來源:轉載
供稿:網友
   在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 BaseClass() {
}
public void check(String name){
if (name.equals("supervisor"))
System.out.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);
}
}



  好了,下面做一個通知類(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將被調用。




上一篇:SpringAOP之HelloWorld

下一篇:SpringReference

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 牙克石市| 青冈县| 凭祥市| 普陀区| 尼木县| 韩城市| 东光县| 唐河县| 新民市| 砚山县| 从化市| 故城县| 东辽县| 云梦县| 拉萨市| 黄大仙区| 榆社县| 龙井市| 青海省| 江津市| 甘孜县| 西峡县| 遵化市| 右玉县| 萨迦县| 齐河县| 中卫市| 蒙阴县| 万宁市| 渭南市| 天镇县| 房产| 修水县| 陆川县| 施秉县| 措美县| 集安市| 太湖县| 盐津县| 苍南县| 静宁县|