用过Spring MVC的人都知道,我们如何在Controller中注入Service,可以使用@Resource注解的方法。

有时候,实际在项目的过程中,我们需要在某个Servlet中使用Service, 但是由于Spring MVC中的Servlet都是由

DispatcherServlet统一管理的,因此,像Controller方式的注解方式注入在普通的Servlet中是行不通的。

本文介绍通过实现ApplicationContextAware的方法在你自己的Servlet中也可以很轻松地使用你的Service。

首先,你需要实现你的Spring上下文工具类,代码如下:

package com.tg.util.web;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; /**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} /**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
} /**
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
* @param name bean注册名
* @param requiredType 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
} /**
* 如果BeanFactory包含所给名称匹配的bean返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
} /**
* 判断注册的bean是singleton还是prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
} /**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
} /**
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}

第二步非常重要,你需要在你的Spring配置文件中加入你的工具类Bean的单例配置,代码如下:

<beans ...
<bean id="SpringContextUtil " class="com.tg.util.web.SpringContextUtil " scope="singleton" />
</beans>

最后一步就是使用了,代码如下:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
GameInfoService service = (GameInfoService) SpringContextUtil.getBean("gameInfoService");
List<GameInfo> games = service.getAll();
...
}

好了,一切大功告成!

Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service的更多相关文章

  1. servlet中调用注入spring管理的dao(转)

    今天做大型仪器的的时候遇到的问题,转下为了以后能用 http://blog.csdn.net/jiyingying_up/article/details/44803585 我们用spring的依赖注入 ...

  2. Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)

    当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...

  3. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

  4. 关于jpa的Specification自定义函数,实现oracle的decode;以及如何在静态方法中调用注入的service

    如何在静态方法中调用注入的service Public class ClassA{ public static ClassA classA; @Resource private Service ser ...

  5. springboot 项目普通类中调用mapper或service接口

    1.该类使用@Component注解 2.添加一个本类类型的静态字段 3.创建一个初始化方法,贴上@PostConstruct 标签,用于注入bean 4.创建方法调用mapper或service接口 ...

  6. 静态工具类中使用注解注入service

    转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...

  7. 静态工具类中使用注解注入service实例

    一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package ...

  8. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  9. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

随机推荐

  1. 基于设备树的TQ2440 DMA学习(1)—— 芯片手册

    作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...

  2. 在ASP.NET MVC中使用Knockout实践01,绑定Json对象

    本篇体验在ASP.NET MVC下使用Knockout,将使用EF Code First创建数据库.最后让Knockout绑定一个Json对象. 创建一个领域模型. namespace MvcAppl ...

  3. 架构:Introducing Expert Systems and Distributed Architecure

    原文地址:http://www.yourenterprisearchitect.com/2011/10/introducing-service-bus.html.   Expert Systems. ...

  4. 老美的zxing和日本的qrcode哪个好?

    ZXing用Java实现的多种格式的1D/2D条码图像处理库,Zxing库的主要部分支持以下几个功能:核心代码的使用.适用于J2SE客户端的版本.适用于Android客户端的版本(即BarcodeSc ...

  5. Java-----隐藏手机号中间四位,身份证号码中间几位

    phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");152****4799 idCard.replace ...

  6. Adapter数据变化改变现有View的实现原理及案例

    首先说说Adapter详细的类的继承关系.例如以下图 Adapte为接口它的实现类的对象作为AdapterView和View的桥梁,Adapter是装载了View(比方ListView和girdVie ...

  7. 应用内截屏的代码,在Activity中测试可用

    截屏功能让我十分头疼,想做个无需root的又找不到资料.这里暂且分享一个无需root的,在应用内截屏的代码,本文转自:http://blog.csdn.net/csh159/article/detai ...

  8. Android中获取屏幕长宽的方法

    package com.kale.screen; import android.annotation.SuppressLint; import android.app.Activity; import ...

  9. LaTeX技巧24:LaTeX常用命令集锦

    \hyphenation{word list} %断字命令:\showthe\topmargin %显示某个参数的数值或者内容: 在tex编译过程中出现行溢出(overflow hbox)是由于断字程 ...

  10. 聚类:层次聚类、基于划分的聚类(k-means)、基于密度的聚类、基于模型的聚类

    一.层次聚类 1.层次聚类的原理及分类 1)层次法(Hierarchicalmethods)先计算样本之间的距离.每次将距离最近的点合并到同一个类.然后,再计算类与类之间的距离,将距离最近的类合并为一 ...