将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 ...
随机推荐
- Kernel启动时 驱动是如何加载的module_init,加载的次序如何;略见本文
Init.h中有相关initcall的启动次序,在system.map中可看出具体的__initcall指针的前后次序 #define pure_initcall(fn) __define_initc ...
- 数字证书私钥sign及验证
package com.epay.bank.test.encrypt; import java.io.FileInputStream; import java.security.KeyStore; i ...
- c++のdll两种调用方式
调用DLL有两种方法:静态调用和动态调用. (一).静态调用其步骤如下: 1.把你的youApp.DLL拷到你目标工程(需调用youApp.DLL的工程)的Debug目录下; 2.把你的youApp. ...
- Mysql--学习笔记(==》简单查询三)
-- 查看查询数据显示SELECT * FROM student; -- 显示一部分信息的查询SELECT sname 姓名,sscore 成绩,saddress 家庭住址 FROM student; ...
- 如何在安装32位Oracle客户端组件的情况下以64位模式运行
C#使用System.Data.OracleClient连接Oracle数据库.之前在WinXP上正常运行的程序移植到Windows 2008 x64上之后就连不上数据库了,错误信息如下:启动data ...
- maven入门教程
一.Maven 是什么? Maven 英文名称:专家,由名字可见,这个工具野心非常大.有人说他是"构建工具",一个用来把源代码构建成可发布的构件的工具.也有人说是项目管理工具. 官 ...
- Cheatsheet: 2013 07.21 ~ 07.31
Mobile Android vs. iOS: Comparing the Development Process of the GQueues Mobile Apps Android Studio ...
- git生成SSH key
使用https:// 提交GitHub上的私有项目时,需要每次都输入帐号和密码,比较麻烦. 比如我自己的笔记本,在push的时候我可不想每次都输入这些. 那就使用SSH吧,这个需要在GitHub的帐号 ...
- CUBRID学习笔记 8 复制数据库
1 export database 类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...
- python内置函数的归集
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...