此文章是基于  搭建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。

spring服务定位器类的更多相关文章

  1. Lind.DDD.IoC(大叔推荐)~在服务定位器中引入IoC容器~容器的适配器

    回到目录 关于依赖倒置(DIP) 高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口,通俗的讲,就是高层模块定义接口,低层模块负责实现,这在我们实际开发中经常被用到,层与层之间引用,经 ...

  2. YII框架的依赖注入容器与服务定位器简述

    依赖注入容器 依赖注入(Dependency Injection,DI)容器就是一个对象use yii\di\Container,它知道怎样初始化并配置对象及其依赖的所有对象. 依赖注入和服务定位器都 ...

  3. 服务定位器(Service Locator)

    服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...

  4. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  5. Java服务定位器模式

    当我们想要使用JNDI查找来定位各种服务时,使用服务定位器设计模式. 考虑到为服务查找JNDI的高成本,所以在服务定位器模式使用缓存技术. 首次需要服务时,服务定位器在JNDI中查找并缓存服务对象. ...

  6. 避免在ASP.NET Core中使用服务定位器模式

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:服务定位器(Service Locator)作为一种反模式,一般情况下应该避免使用,在 ...

  7. Spring Boot普通类调用bean

    1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...

  8. Android - 位置定位(Location)服务(Service)类的基本操作

    位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...

  9. Spring @Aspect进行类的接口扩展

    Spring @Aspect进行类的接口扩展: XML: <?xml version="1.0" encoding="UTF-8"?> <be ...

随机推荐

  1. Rest风格中关于JPA使用懒加载的坑

    公司最近使用的ORM框架是JPA实现产品使用的是hibernate,曾经看过一篇博客上面说的是如果团队里面没有一个精通hibernate的人,那么最好不要使用它,我现在是深刻的体会到了.但是使用什么框 ...

  2. 形象化的spring 依赖注入原理

      转. IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制.这称为控制反转. 下面我们以几个例子来说明什 ...

  3. JAVASCRIPT实现网页版:俄罗斯方块

    HTML+CSS+JS实现俄罗斯方块完整版,素材只有图片,想要的下载图片按提示名字保存,css中用的时候注意路径!!主要在JS中!JS附有详细注释 效果: 按键提示:[键盘按键] 素材:图片名字与代码 ...

  4. CSS3绘制六边形

    因为很简单,所以先总结一下:使用CSS3绘制六边形主要使用伪类:before和:after在源元素之前和之后再绘制两个元素,并利用css3的边框样式,将这两个元素变成三角形放置在源元素的两端即可. ( ...

  5. 一步一步教你如何解锁被盗的iPhone 6S

    即使你的iPhone6S设置了六位数的密码,甚至还设置了touch ID,但我要告诉你的是:你的手机仍然能被犯罪分子解锁. 事件背景 三天前,一位苹果用户的iPhone6S被偷了.随后,小偷重置了该用 ...

  6. Office 365 - SharePoint 2013 Online 中创建母版页

    1.登陆SharePoint Online站点,点击右上角的设置按钮,如下图: 2.点击进入网站设置,到下面两个地方开启SharePoint Server 发布基础架构: 网站集管理 – 网站集功能 ...

  7. Arcgis创建SDE_Geometry、SDO_Geometry的区别【转】

    1. SDO_GEOMETRY Oracle Spatial在MDSYS模式下定义了一系列几何类型.函数来支持空间数据的存储和使用,最为人耳熟能详的就是SDO_GEOMETRY这种类型——当然,Arc ...

  8. linux下发布的执行文件崩溃的问题定位 心得一则

    C++ Release版本发布到客户处执行时,如果程序崩溃,有什么办法能够快速的确认程序的问题呢? 如果能gdb调试的话,比较简单了,可以使用gdb命令,类似如下: gdb ##set args ** ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q45-Q48)

    Question 45 You are designing a branding strategy for a customer with a new SharePoint 2010 server f ...

  10. Servlet基础(三) Servlet的多线程同步问题

    Servlet基础(三) Servlet的多线程同步问题 Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率. 由于Servlet/JSP默认是以多线程模式执行的, ...