Struts Action服務(wù)定位器
本文向大家介紹怎樣配置一個服務(wù)定位器的例子,可能好多人還不了解Struts Action服務(wù)定位器,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。
Struts Action提供一個服務(wù)定位器
既然我們已經(jīng)把我們的服務(wù)和我們的DAO連起來了,我們需要把我們的服務(wù)暴露給其它層。通常是一個像使用Struts或Swing這樣的用戶接口層里的代碼來使用這個服務(wù)。一個簡單的處理方法是使用一個服務(wù)定位器模式的類從一個Spring上下文中返回資源。這也可以靠引用bean ID通過Spring來直接完成。
這兒是一個在Struts Action中怎樣配置一個服務(wù)定位器的例子:
- public abstract class BaseAction extends Action {
- private IOrderService orderService;
- public void setServlet(ActionServlet actionServlet) {
- super.setServlet(actionServlet);
- ServletContext servletContext = actionServlet.getServletContext();
- WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext( servletContext);- this.orderService = (IOrderService)
- wac.getBean("orderService");
- }
- protected IOrderService getOrderService() {
- return orderService;
- }
- }
【編輯推薦】