Servlet自动注入Spring容器中的Bean解决方法
很多情况在进行Web开发的时候需要自己手写Servlet来完成某些功能,而servlet有需要注入Spring容器中的某些bean,这是每次都要手动获取比较麻烦,这里有一个解决方案,只需要写一个servlet的基类,其它的类只需要集成基类后,便可以想action那样进行注入了。
基类Servlet代码如下:
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
super.init();
WebApplicationContextUtils
.getWebApplicationContext(getServletContext())
.getAutowireCapableBeanFactory().autowireBean(this);
}
}
具体的功能servlet代码如下:
@WebServlet("/testServlet")
public class TestServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
@Autowired
private TestService testService = null;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
testService.print();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
其中testServic属性便是自动注入的。具体的spring配置注入不再细说。
Servlet自动注入Spring容器中的Bean解决方法的更多相关文章
- SpringBoot中普通类无法通过@Autowired自动注入Service、dao等bean解决方法
无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com ...
- Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...
- JSP页面中如何注入Spring容器中的bean
第一步在JSP页面中导入下面的包: <%@page import="org.springframework.web.context.support.WebApplicationCont ...
- SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean
SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
随机推荐
- 队列(循环队列)----C语言
线性结构:有且只有一个根节点,且每个节点最多有一个直接前驱和一个直接后继的非空数据结构 非线性结构:不满足线性结构的数据结构 队列 队列一般分为两类:链式队列和顺序队列 链式队列---链式队列即用链表 ...
- PHP中日期函数
1,转化为时间戳函数:strtotime() 本函数接受一个包含美国英语日期格式的字符串并尝试将其解析为Unix时间戳,其值相对于now参数给出的时间,如果没有提供此参数则使用系统当前时间. < ...
- Python小白学习之路(十七)—【内置函数二】
序列操作类函数 all() 功能:判断可迭代对象的每个元素是否都为True值注意:If the iterable is empty, return True.(举例3) 回顾:None '' ...
- 【learning】微信跳一跳辅助c++详解 轻松上万 【上】
写在前面 17年年底Wechat出了这个跳一跳的小游戏,今年2月份的时候简单地玩了一下,发现被游戏虐了(手太残了只能跳20多). 今天刚好有点空,于是就花了一个下午的时间写了一个跳一跳的c++ ...
- 转MVC3介绍
第一节:Asp.Net MVC3项目介绍 让我们先看一下,一个普通的Asp.Net MVC3项目的样例,如下图所示 跟WebFrom还是有区别的,如果你已经了解Asp.Net MVC2的话,那就感觉异 ...
- 搭建互联网架构学习--003--maven以及nexus私服搭建
跳过,等待完善中,,, 后台服务工具maven:使用Nexus配置Maven私有仓库 一.安装配置Nexus 1. 下载nexus https://www.sonatype.com/download- ...
- 【树】Path Sum II(递归)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Elasticsearch入门(一)
索引(index) -- 存储关联数据的地方.实际上,索引只是一个逻辑命名空间(logical namespace),它指向一个或多个分片(shards). 分片(shard) 是 工作单元(work ...
- 如何自学计算机科学与技术(Teach Yourself Computer Science)
如果你是一个自学成才的半吊子,或者是从培训班毕业的小菜鸟.那么,你欠自己一份计算机科学的专业知识.感谢上帝,如今你可以接受世界水平的计算机教育,但无需花费数年时间和一笔不小的金钱. 互联网上的学习资源 ...
- Python2.x 中文乱码问题
Python 文件中如果未指定编码,在执行过程会出现报错: #!/usr/bin/pythonprint "你好,世界"; 以上程序执行输出结果为: File "test ...