国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Spring AOP使用配置介紹(二):切面的配置
在文章(一)中已經介紹了增強的使用,此篇介紹切面的配置方法。

創(chuàng)建靜態(tài)普通方法名匹配的切面:
Java代碼  
  1. package com.maxjay.bank.advice;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.springframework.aop.ClassFilter;  
  6. import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;  
  7.   
  8. import com.maxjay.bank.service.UserService;  
  9.   
  10. /** 
  11.  * 靜態(tài)普通方法名匹配切面定義 
  12.  * @author Administrator 
  13.  * 
  14.  */  
  15. public class ValidateUserAdvisor extends StaticMethodMatcherPointcutAdvisor {  
  16.   
  17.     /** 
  18.      *  
  19.      */  
  20.     private static final long serialVersionUID = 1L;  
  21.   
  22.     /** 
  23.      * 只對validateUser進行攔截 
  24.      */  
  25.     public boolean matches(Method method, Class targetClass) {  
  26.         // 此次可以使用正則表達式來匹配  
  27.         return "validateUser".equals(method.getName());  
  28.     }  
  29.   
  30.     public ClassFilter getClassFilter() {  
  31.         return new ClassFilter() {  
  32.             public boolean matches(Class clazz) {  
  33.                 return UserService.class.isAssignableFrom(clazz);  
  34.             }  
  35.         };  
  36.     }  
  37. }  

該類中的matches方法用來確定哪些方法需要執(zhí)行增強中的內容;getClassFilter則確定哪些類需要被代理,這個方法在使用自動創(chuàng)建代理時會很有用。

在spring配置文件中配置切面:
Java代碼  
  1. <bean id="validateUserAdvisor" class="com.maxjay.bank.advice.ValidateUserAdvisor">  
  2.     <property name="advice">  
  3.         <ref bean="loggerBeforeAdvice" />  
  4.     </property>  
  5. </bean>  

其中l(wèi)oggerBeforeAdvice是(一)中已經定義過的增強。
配置代理類:
Java代碼  
  1. <!--  
  2.     配置單個bean的代理,在使用時不能用原有bean的id要用AppContext.get("singleLoginProxy")從context中獲取(見測試類LoggerAdviceTest)  
  3. -->  
  4. <bean id="singleLoginProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  5.   
  6.     <property name="proxyInterfaces">  
  7.         <value>com.maxjay.bank.service.UserService</value>  
  8.     </property>  
  9.   
  10.     <property name="interceptorNames">  
  11.         <list>  
  12.              <value>validateUserAdvisor</value>                           
  13.         </list>  
  14.     </property>  
  15.   
  16.     <property name="target">  
  17.         <ref bean="userService" />  
  18.     </property>  
  19.   
  20.     <!-- 設置是否直接代理類(默認為false):true即使用cglib生成代理類,此時target對象不可以是JDK動態(tài)代理過的bean;false則使用JDK動態(tài)代理 -->  
  21.     <property name="proxyTargetClass">  
  22.         <value>true</value>  
  23.     </property>  
  24. </bean>  

注意此處配置與(一)中只有interceptorNames換成了我們剛剛配置的那個切面。

此外也可以使用靜態(tài)正則表達式方法匹配來創(chuàng)建切面,代碼如下
Java代碼  
  1. <!-- 定義靜態(tài)正則表達式方法名匹配切面 -->  
  2. <bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  3.     <property name="patterns">  
  4.         <list>  
  5.             <value>.*get.*</value>  
  6.         </list>  
  7.     </property>  
  8.     <property name="advice">  
  9.         <ref bean="loggerBeforeAdvice" />  
  10.     </property>  
  11. </bean>  

其中.*get.*是標準的正則表達式語言,它匹配那些名稱種包含get的方法
       通過上面的步驟我們就定義完了切面了。
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring學習手札(二)面向切面編程AOP
Sping2.0 + Struts2.0 +Ibatis 的使用總結
面試官:Spring Aop 與 AspectJ 有什么區(qū)別和聯(lián)系?
Spring AOP原理及攔截器
Spring面向切面
Spring AOP 常用的四種實現(xiàn)方式 - Spring之旅 - JavaEye技術社區(qū)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服