Junky's IT Notebook

          統計

          留言簿(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 閱讀(1412) 評論(0)  編輯  收藏 所屬分類: spring

          主站蜘蛛池模板: 定南县| 万安县| 公主岭市| 枣庄市| 富民县| 博客| 曲周县| 达拉特旗| 调兵山市| 依兰县| 龙井市| 民丰县| 贡嘎县| 陆川县| 霞浦县| 福海县| 姜堰市| 南涧| 大余县| 宁城县| 湘潭县| 常山县| 德昌县| 磐安县| 平原县| 论坛| 高平市| 密山市| 临清市| 钟山县| 陆河县| 渭源县| 黄龙县| 吉木萨尔县| 永丰县| 成安县| 泾源县| 防城港市| 定安县| 和龙市| 隆尧县|