将spring管理的bean使用注解的方式注入到servlet中
Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下:
1。创建一个AbstractServlet 抽象类,让你的所有servlet继承于此类:
import java.io.IOException;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.context.support.SpringBeanAutowiringSupport;
- /**
- * Servlet implementation class AbstractServlet
- */
- public class AbstractServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public AbstractServlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- public void init(ServletConfig config) throws ServletException {
- // 在 servlet的 init method中,,寫下面的程式,讓 spring 去 ApplicationContext 做 lookup,
- SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
- config.getServletContext());
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
- * response)
- */
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
- * response)
- */
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
然后需要有注解spring管理的bean的servlet或fiter时候继承此类,重写方法
将spring管理的bean使用注解的方式注入到servlet中的更多相关文章
- 为何在新线程中使用注解获取不到Spring管理的Bean
新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...
- 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean
BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...
- 手动获取被spring管理的bean对象工具
在netty handler开发中,我们无法将spring的依赖注入到Handler中,无法进行数据库的操作,这时候我们就需要手动获取被spring管理的bean对象: 创建一个 imp ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- (转)配置Spring管理的bean的作用域
http://blog.csdn.net/yerenyuan_pku/article/details/52833477 Spring管理的bean的作用域有: singleton 在每个Spring ...
- 【Spring注解驱动开发】面试官:如何将Service注入到Servlet中?朋友又栽了!!
写在前面 最近,一位读者出去面试前准备了很久,信心满满的去面试.没想到面试官的一个问题把他难住了.面试官的问题是这样的:如何使用Spring将Service注入到Servlet中呢?这位读者平时也是很 ...
- spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
- servlet中用注解的方式读取web.xml中的配置信息
在学习servletContext的时候,我们知道了可以在web.xml中通过<context-param>标签来定义全局的属性(所有servlet都能读取的信息),并在servlet中通 ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
随机推荐
- linux命令总结2
昨天继续对239进行挖掘,想把运营登录浮层的示例页面放在这台测试机上,供大家使用,结果在配置apache时出现了问题,无论怎样,页面都是403 Forbidden,最后终于被露颖同学经过2个小时的努力 ...
- 异步I/O
http://blog.csdn.net/gotosola/article/details/7412409 Linux2.6异步I/O AIO的基本思想: 允许进程发起很多I/O操作,而不用阻塞或等 ...
- yum mysql on centos 7
参考:https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-centos-7 centos 7上没有办法使用yum i ...
- jquery的ajax向ashx传值,中文乱码问题
从网上查找了很多资料: 有在配置文件里面加如下配置 <globalization responseEncoding="utf-8" requestEncoding=" ...
- ci 基础知识
//建立虚拟主机 2.下载完毕后,解压,并将其复制粘贴到程序的主目录(d:/wamp/xmall/),然后再将其重命名为"myshop",那么"myshop" ...
- AJAX文档
AJAX 文档 AJAX开发简略.................................................................................... ...
- RabbitMQ术语
工作队列:Working Queue 分配:多个客户端接收同一个Queue,如何做负载均衡(分配). Round-robin分配:多个接收端接收同一个Queue时,采用了Round-robin ...
- 3.linux man手册
(12) man作用:查询man手册,获得帮助信息man 1 ls 1表示查询的是linux命令man 2 xxx 2表示查询的是linux apiman 3 xxx 3表示查询的是C库函数注意:在m ...
- 不同分辨率的LCM进行兼容
1.读取不同LCM ID的时序不同,如何处理? 2.开机后android会自动resize图片资源的大小,但开机logo无法自行resize,如何操作?保证识别到不同分辨率的LCM后开机logo可以正 ...
- RabbitMQ在CentOS上的简单安装配置
安装 1.依赖Erlang,yum install erlang安装之 2.去官网下载Fedora/RHEL的rpm包,rpm -ivh rabbitmq-server-*.noarch.rpm 安装 ...