Junky's IT Notebook

          導航

          <2006年10月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          隨筆分類(224)

          文章分類(5)

          隨筆檔案(228)

          文章檔案(7)

          統計

          留言簿(8)

          積分與排名

          WebSphere Studio

          閱讀排行榜

          評論排行榜

          ServiceLocator的實現

          ServiceLocator的實現

          /*

          * Created on 2004-8-25 by simba.

          *

          */

          package com.simba.blog.util;

          ?

          ?

          import javax.servlet.ServletContext;

          ?

          import org.apache.commons.logging.Log;

          import org.apache.commons.logging.LogFactory;

          import org.springframework.context.ApplicationContext;

          import org.springframework.web.context.support.WebApplicationContextUtils;

          ?

          import com.opensymphony.webwork.ServletActionContext;

          import com.simba.blog.service.BlogService;

          ?

          /**

          * @author simba

          *

          * email: simbasun@msn.com

          */

          public class ServiceLocator

          {

          //the catalog service bean name

          private static final String BLOG_SERVICE_BEAN_NAME = "blogService";

          ?

          //the user service bean name

          private static final String USER_SERVICE_BEAN_NAME = "userService";

          ?

          //the logger for this class

          private Log logger = LogFactory.getLog(this.getClass());

          ?

          //the Spring application context

          private ApplicationContext appContext;

          ?

          //the cached catalog service

          private BlogService blogService;

          ?

          //the cached user service

          //private UserService userService;

          ?

          /**

          * Constructor.

          * <p>

          * The following steps being done:

          * <ul>

          * <li>retrieve Spring application context from servlet context.

          * <li>look up <code>CatalogService</code> from Spring application

          * context.

          * <li>look up <code>UserService</code> from Spring applicatino context.

          * </ul>

          */

          public ServiceLocator()

          {

          /*InputStream is = getClass().getResourceAsStream("springapp-servlet.xml");

          XmlBeanFactory bf = new XmlBeanFactory(is);

          blogService = (BlogService) bf.getBean("blogService");*/

          ?

          ServletContext context = ServletActionContext.getServletContext();

          this.appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

          this.blogService = (BlogService)this.lookupService(BLOG_SERVICE_BEAN_NAME);

          ?

          /*

          * this.userService = (UserService)this.lookupService(USER_SERVICE_BEAN_NAME);

          */

          ?

          this.logger.info("Service locator bean is initialized");

          }

          ?

          ?

          /**

          * Lookup service based on service bean name.

          *

          * @param serviceBeanName the service bean name

          * @return the service bean

          */

          public Object lookupService(String serviceBeanName)

          {

          return appContext.getBean(serviceBeanName);

          }

          ?

          /**

          * @return Returns the blogService.

          */

          public BlogService getBlogService()

          {

          return blogService;

          }

          }

          ?

          ?

          ?

          [in web.xml]

          ?

          <context-param>

          <param-name>contextConfigLocation</param-name>

          <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>

          </context-param>

          <listener>

          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

          </listener>

          <!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER

          <servlet>

          <servlet-name>context</servlet-name>

          <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

          <load-on-startup>1</load-on-startup>

          </servlet>

          -->

          posted on 2006-10-17 14:06 junky 閱讀(1414) 評論(0)  編輯  收藏 所屬分類: spring

          主站蜘蛛池模板: 江都市| 儋州市| 类乌齐县| 板桥市| 长治市| 土默特左旗| 治多县| 普安县| 海南省| 桐柏县| 比如县| 石门县| 桑日县| 深州市| 辉县市| 深圳市| 沈丘县| 富民县| 晴隆县| 千阳县| 泸定县| 赤水市| 米易县| 富民县| 炎陵县| 句容市| 柞水县| 昌都县| 海阳市| 石嘴山市| 潼南县| 邳州市| 台东县| 耒阳市| 北辰区| 芦山县| 外汇| 友谊县| 乳山市| 城口县| 哈密市|