spring服务定位器类

此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

功能:通过持有的Spring应用场景ApplicationContext,可在任何地方获取bean。

1. 服务定位器类:ServiceLocator.java

 package com.ims.common;

 import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; /**
* 服务定位器
* 持有Spring的应用场景, 可在任何地方获取bean.
*/
public final class ServiceLocator implements ApplicationContextAware, DisposableBean { private static Logger logger = Logger.getLogger(ServiceLocator.class);
private static ApplicationContext context = null; /**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
* @param context
*/
@Override
public void setApplicationContext(ApplicationContext context) {
logger.debug("Injected the ApplicationContext into ServiceLocator:" + context);
if (ServiceLocator.context != null) {
logger.debug("[------------ ApplicationContext in the ServiceLocator " +
"is covered, as the original ApplicationContext is:"
+ ServiceLocator.context + " ------------]");
}
ServiceLocator.context = context;
} /**
* 实现DisposableBean接口,在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
ServiceLocator.clear();
} /**
* 取得存储在静态变量中的ApplicationContext.
* @return
*/
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return context;
} /**
* 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型.
* @param name bean名称
* @return bean对象
*/
@SuppressWarnings("unchecked")
public static <T> T getService(String name) {
assertContextInjected();
return (T) context.getBean(name);
} /**
* 从Spring的应用场景中取得Bean, 自动转型为所赋值对象的类型.
* @param requiredType bean类
* @return bean对象
*/
public static <T> T getService(Class<T> requiredType) {
assertContextInjected();
return context.getBean(requiredType);
} /**
* 清除ServiceLocator中的ApplicationContext
*/
public static void clear() {
logger.debug("Clear ApplicationContext in ServiceLocator :" + context);
context = null;
} /**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
if (context == null) {
throw new IllegalStateException("ApplicaitonContext not injected, " +
"as defined in the context.xml ServiceLocator");
}
}
}

2. Spring上下文场景加载监听器:SpringContextLoaderListener.java

 package com.ims.web;

 import javax.servlet.ServletContextEvent;

 import org.springframework.web.context.ContextLoaderListener;

 import com.ims.common.ServiceLocator;

 /**
* Spring场景加载监听器,用于加载/销毁Spring场景
*/
public class SpringContextLoaderListener extends ContextLoaderListener { private final ServiceLocator locator = new ServiceLocator(); public SpringContextLoaderListener() {
super();
} @Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event); // 将Spring场景置入服务定位器中
locator.setApplicationContext(getCurrentWebApplicationContext());
} @Override
public void contextDestroyed(ServletContextEvent event) {
try {
locator.destroy();
}
catch (Exception e) {
e.printStackTrace();
} super.contextDestroyed(event);
}
}

3. 修改 web.xml

如果存在如下代码,则去掉

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

然后添加上如下代码:

<listener>
<listener-class>com.ims.web.SpringContextLoaderListener</listener-class>
</listener>

4. 在 src/applicationContext.xml 中添加如下代码:

<bean class="com.ims.common.ServiceLocator"/>

5. 调用 ServiceLocator.java 中的 getService 函数,可分为按名称或类型获取bean。

上一篇:zw·准专利·高保真二值图细部切分算法


下一篇:iOS开发之UIImage等比缩放