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

          主站蜘蛛池模板: 阳高县| 萨迦县| 固阳县| 定南县| 图们市| 游戏| 都江堰市| 三门峡市| 天峨县| 营口市| 宁海县| 湖口县| 镇江市| 沂南县| 阳谷县| 姜堰市| 丰城市| 灌云县| 洮南市| 宣化县| 沾益县| 菏泽市| 唐海县| 松桃| 甘孜县| 葫芦岛市| 北安市| 大方县| 高密市| 西贡区| 南平市| 九龙县| 房产| 吕梁市| 东安县| 湟源县| 邢台县| 达拉特旗| 商丘市| 简阳市| 马龙县|