用过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();
...
}
好了,一切大功告成!
分享到:
相关推荐
web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware ...
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
4.6.1.6 在非Web 应用中,优雅地关闭Spring IoC 容器 74 4.6.2 ApplicationContextAware 和BeanNameAware . 75 4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1...
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
ApplicationContextAware当前的applicationContext,这也可以调用容器的服务 4. Bean的自动装配 byName根据属性名称自动装配-set注入 byType在容器中查找指定属性类型进行装配-set注入 构造函数与byType类似-构造...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
自定义标签中@Autowired的属性...1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加<bean id="" class="com............SpringContext"> 3.使用SpingContext.getBean("bean名");获取
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
Spring boot 基础 1 1.什么是spring boot 1 2.spring boot的优点 2 3.环境搭建 3 3.1 添加依赖 3 3.2 创建目录和配置文件 4 3.3 创建启动类 5 3.4 案例演示 5 4 . 配置介绍 6 4.1 Spring boot 配置文件 6 4.2 ...
为何@Autowired可以注入ApplicationContext, 一般来说,我们可以通过实现ApplicationContextAware接口来获取ApplicationContext的引用。但是根据官方文档,发现也可以通过 @Autowired来注入ApplicationContext,这...
ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ...
在笔者的一个项目中,就曾经借助事件机制,较好的实现了当系统 异常时在监视终端上报警,同时发送报警SMS至管理员手机的功能。 */ public class LoginAction implements ApplicationContextAware { private ...
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
1.继承ApplicationContextAware 2.代码如下 3.记得要把这个工具类加入到Spring的扫描路径下,这样才能在初始化时注入 4.使用方法