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。
spring服务定位器类的更多相关文章
- Lind.DDD.IoC(大叔推荐)~在服务定位器中引入IoC容器~容器的适配器
回到目录 关于依赖倒置(DIP) 高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口,通俗的讲,就是高层模块定义接口,低层模块负责实现,这在我们实际开发中经常被用到,层与层之间引用,经 ...
- YII框架的依赖注入容器与服务定位器简述
依赖注入容器 依赖注入(Dependency Injection,DI)容器就是一个对象use yii\di\Container,它知道怎样初始化并配置对象及其依赖的所有对象. 依赖注入和服务定位器都 ...
- 服务定位器(Service Locator)
服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- Java服务定位器模式
当我们想要使用JNDI查找来定位各种服务时,使用服务定位器设计模式. 考虑到为服务查找JNDI的高成本,所以在服务定位器模式使用缓存技术. 首次需要服务时,服务定位器在JNDI中查找并缓存服务对象. ...
- 避免在ASP.NET Core中使用服务定位器模式
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:服务定位器(Service Locator)作为一种反模式,一般情况下应该避免使用,在 ...
- Spring Boot普通类调用bean
1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...
- Android - 位置定位(Location)服务(Service)类的基本操作
位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...
- Spring @Aspect进行类的接口扩展
Spring @Aspect进行类的接口扩展: XML: <?xml version="1.0" encoding="UTF-8"?> <be ...
随机推荐
- 什么是CGI
什么是CGI 1. 定义: CGI(Common Gateway Interface)是HTTP服务器与你的或其它机器 上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. 2. 功能: 绝大 ...
- Guava学习笔记:Preconditions优雅的检验参数
在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常流程执行下去.对于可预知的一些数据上的错误,我们一定要做事前检测和判断,来避免程序流程出错,而不是完全通过错误处 ...
- MYSQL 解锁与锁表
解锁 第一种 show processlist; 找到锁进程,kill id ; 第二种 mysql>UNLOCK TABLES; 锁表 锁定数据表,避免在备份过程中,表被更新 mysql> ...
- Spring4学习笔记1-HelloWorld与IOC和DI概述
1.安装环境 1.1安装eclipse,jdk 1.1安装Spring tool suite(非必要项) 2.spring HelloWorld 2.1 需要的jar包(spring官网下载:http ...
- PCL -语法错误:“::” error C2589: “(”:“::”右边的非法标记
1.错误原因:系统函数与pcl中的max函数冲突导致的 2.两种解决办法: 1)错误中max和min函数用括号括起来,例如"std::Max"修改为“(std::Max)”. 2) ...
- ENVI【遥感图像预处理之图像的几何校正】
---恢复内容开始--- 一.图像几何校正的概述 1.几何校正方法: 1)利用卫星自带的地理定位文件进行几何校正.主菜单>>>Map>>Georeference传感器的名 ...
- Plyr – 简单,灵活的 HTML5 媒体播放器
Plyr 是一个简单的 HTML5 媒体播放器,包含自定义的控制选项和 WebVTT 字幕.它是只支持现代浏览器,轻量,方便和可定制的媒体播放器.还有的标题和屏幕阅读器的全面支持. 在线演示 ...
- 详细讲解nodejs中使用socket的私聊的方式
详细讲解nodejs中使用socket的私聊的方式 在上一次我使用nodejs+express+socketio+mysql搭建聊天室,这基本上就是从socket.io的官网上的一份教程式复制学习,然 ...
- cordova 添加闪屏效果
为项目添加SplashScreen插件 在Cordova项目目录运行: cordova plugin add apache.cordova.splashscreen 这个命令从插件git库下载插件代码 ...
- Android Testing学习01 介绍 测试测什么 测试的类型
Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...