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

打開APP
userphoto
未登錄

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

開通VIP
Struts調(diào)用Spring服務類的三種方法
Struts調(diào)用Spring服務類的三種方法
2008-10-29 11:33:35
標簽:STRUTS SPRING
 
用SSH做了幾個項目,現(xiàn)在總結(jié)一下Struts  Action中調(diào)用Spring  Service的方法,大家有好的實現(xiàn),請繼續(xù)補充:

1.老爸操持型  
這種類型,即是在BaseAction中提供一個getBean(String  beanName)的父類方法,業(yè)務Action  在需要Serivce時,調(diào)用父類的getBean()得到Object型的Service,再Cast。  
e.g.  


代碼
程序代碼:
public class BaseAction extends DispatchAction   
{   
   ...    
   public Object getBean(String name)   
  {   
      if (ctx == null)   
      {   
          ctx = WebApplicationContextUtils   
                .getRequiredWebApplicationContext(servlet.getServletContext());   
      }   
      return ctx.getBean(name);   
  }   
}  


2.自已動手型  
自己動手  豐衣足食,再加了自從有了IoC,所謂動手也只是衣來后的伸手和飯來后的張口。自己動手型,即是利用Spring的IoC,將IoC容器中的Service注入到Action中,當然Action需要提供注入服務有setter.  
如果采用這種方式,首先需要讓Spring接管Struts,然后在Spring配置文件中,配置Action的注入屬性。  
首先.在struts-config.xml將Spring引狼入室。  


代碼

程序代碼:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">    
        <set-property property="contextConfigLocation"    
                                  value="/WEB-INF/action-servlet.xml"    
        />    
</plug-in>   



然后,Struts再向Spring投懷送抱:  


程序代碼:
<bean id="userService" class="com.nic.service.UserServiceImpl">  
    <property name="userDao">  
    <ref bean="userDao" />  
    </property>  
</bean>    
<bean name="/userAction" class="com.stamen.web.UserAction">  
   <property name="userService" ref="userService"/>  
</bean>  



聰明的你一下就看出“/userAction  ”即是com.stamen.web.UserAction在Struts中的配置名。  

3.朋友幫忙型  朋友多了好辦事,凡事都自己動手總有一天會活活累死,所有Action為自己需要的Service提供setter,并且在Spring中注入,好累啊。小學生都在減負了,我們也來為Action減減負吧--提供一個專門  
查找Serivice的ServiceLocator,負責獲取所有的Service,該類為每個Service提供專門的獲得方法,如:  

程序代碼:
 public class ServiceLocator   
  
   private static ApplicationContext factory = null;   
   public static void init(ApplicationContext ctx)   
   {       
       factory = ctx;   
   }   
   public static LogService getLogService()   
   {   
       return (LogService) ServiceLocator.getBean("logService");   
   }   
  
   public static UserService getUserService()   
   {   
       return (UserService) ServiceLocator.getBean("userService");   
   }   
   public static PieeService getPieeService()   
   {   
       return (PieeService) ServiceLocator.getBean("pieeService");   
   }   
   public static PieeGrid getPieeListService()   
   {   
       return (PieeGrid) ServiceLocator.getBean("pieeListService");   
   }   
   ...   
}   


Action要用哪個Serivce時,直接通過ServiceLocator.getXxxService()就可以獲得了,省去了  
“老爸操持型”指定Service  名和Cast的繁瑣,比在Spring中IoC來IoC去也來得省心省力。  

下面做一個自己的分析小結(jié):  

1.“老爸操持型”  將serviceName分散到代碼中,到時配置文件中serviceName一改,得牽一毛而動全身->維護性差;且要進行Cast轉(zhuǎn)換,怎一個繁字了得。  

2.“自己動手型”也不好,不但在Action中要添加get/setXxxService代碼,而且還要在Struts和Spring的配置文件中做好群眾的大串連工作,難道我們的配置還不夠多嗎?吃回香豆的,你別又跳出來?。海? 

3.“ 朋友幫忙型”,目前,我認為是比較好的方式,ServiceLocator象一個服務周到,工作到位的房產(chǎn)“中介”,我們要租房子  何必滿街跑???TMD  這個月我又交了3K房租,也不知道李大倫,李金寶之流規(guī)矩后,房價能不能降些,我想有個蝸牛的家  已經(jīng)唱了好幾年了--跑題了,睡覺去了:)
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
幾種獲得spring里注冊Bean的方法
Spring在web項目中配置的幾種方式的分析
SpringBoot 非 Spring 管理環(huán)境獲取 Bean
Spring學習筆記:第一章 Spring的下載和安裝
普通Java類獲取Spring的bean
SpringContextUtil ApplicationContextAware static塊中 null
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服