将spring管理的bean使用注解的方式注入到servlet中
2016-09-01 14:49
801 查看
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时候继承此类,重写方法
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-02 spring的bean管理(使用注解方式创建对象 附代码)
- action控制层,使用spring注解方式注入业务bean报空指向错误
- 使用Spring的注解方式注入Struts所管理的action
- Spring的bean管理(注解注入属性和注解文件混合使用)
- Spring中bean管理-配置文件和注解方式混合使用
- Spring 使用注解方式进行事物管理
- Spring 使用注解方式进行事务管理
- servlet 中使用Spring自动注入的bean
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- Spring 使用Spring注解方式管理事务与传播行为
- 学习《Spring实战(第3版)》扎记1:Spring使用注解配置Bean的方式
- 如何使用spring的autowire为servlet注入bean
- Spring 使用注解方式进行事务管理
- SSH下使用Spring注解自动注入bean
- Spring中使用注解的方式注入方式1
- 在Servlet中使用spring注入的bean
- 在Servlet中使用spring注入的bean
- servlet 使用spring管理bean
- Spring 使用注解方式进行事物管理
- 如何使用spring的autowire为servlet注入Bean