此文章是基于  搭建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. 设置placeholder字体颜色

    /*设置placeholder字体颜色*/::-webkit-input-placeholder{ color: #FFF;}:-ms-input-placeholder{ color: #FFF;} ...

  2. 不简单的SQL查询和排序语句

    真不简单!! 一:使用select语句进行查询 语法: SELECT    <列名> FROM      <表名> [WHERE    <查询条件表达式>] [OR ...

  3. jScrollPane 美化滚动条

    在线实例 滚动条可见 滚动条隐藏 使用方法 <div class="container"> <h1>滚动条可见</h1> <div cla ...

  4. 使用 jQuery & CSS3 实现优雅的手风琴效果

    手风琴效果常用于切换显示一组内容,这种方式既可以节省网页空间又可以有动画效果.今天,我们将创造一个优雅的手风琴内容效果.这个想法是有悬停时滑出一些垂直手风琴标签.我们将添加一些 CSS3 属性来提升外 ...

  5. 系统安装LOL等游戏后出现VS调试报错"无法调试""拒绝访问"之类的调试错误

    一个问题抠得脑壳痛,度娘一番各种各样的答案.基本属于 1,调试权限被清0 2,文件权限问题   其中看到很多解决方案中提到"重启电脑"的说法.我也试了几次不行甚至游戏都卸载了.后来 ...

  6. 使用js实现带有停顿效果的图片滚动(按钮控制)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. CSS常用样式(二)

    一.边框样式 1.border:复合属性.设置对象边框的特性. 取值: border-width: 设置或检索对象边框宽度. border-style: 设置或检索对象边框样式. border-col ...

  8. ID卡和IC卡

    1.ID卡 ID卡就是一种身份识别卡,卡内除了卡号之外,无任何加密功能. ID卡的工作原理:它是由卡.读卡器.后台控制器组成的. (1)读卡器通过天线发射射频信号 (2)当卡进入信号范围内后卡被激活 ...

  9. SharePoint 2010升级到sharePoint 2013后,人员失去对网站的权限的原因及解决方法。The reason and solution for permission lost after the upgrading

    昨天碰到了一个问题,一个网站在从SharePoint 2010升级到SharePoint 2013后,人员都不能登录了,必须重加赋权,人员才能登录,这样非常麻烦. 原因:是认证方式的问题.在Share ...

  10. 如何在Infraworks中创建多树种组成的森林

    在Infraworks 2014中,你可以有shp文件导入生成树木和森林,也可以直接在模型中规划一片区域作为森林.美中不足的就是,这些充其量叫树林不能叫森林,因为他们的样式都是一个树种,而真正的森林肯 ...