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 阅读(1406) 评论(0)  编辑  收藏 所属分类: spring


只有注册用户登录后才能发表评论。


网站导航: