将spring管理的bean使用注解的方式注入到servlet中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将spring管理的bean使用注解的方式注入到servlet中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1526字,纯文字阅读大概需要3分钟。
内容图文
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时候继承此类,重写方法
原文:http://www.cnblogs.com/jianwei-dai/p/5829836.html
内容总结
以上是互联网集市为您收集整理的将spring管理的bean使用注解的方式注入到servlet中全部内容,希望文章能够帮你解决将spring管理的bean使用注解的方式注入到servlet中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。