
如何在servlet取得spring beans
13个回答
展开全部
都用spring了为何还要用servlet,用Struts2不好吗。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用webapplicationcontextutils 获取到applicationcontext 后,getbean 就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-03-22 · 知道合伙人软件行家
关注

展开全部
获取ServletContext对象,比如你可以通过重写Servlet的初始化方法init(ServletConfig config), 然后从config中getServletContxt;也可以在处理请求的时候,从Request中getSession().getServletContext()
根据ServletContext,利用Spring的工具类:WebApplicationContextUtils
执行WebApplicationContextUtils.getWebApplicationContext(ServletContext)
拿到了ApplicationContext之后,即拿到了Spring的Bean容器,就可以通过容器获取Bean了,比如现在有一个Bean实现了IHello接口,那么可以通过以下代码获取这个IHello的bean实例: IHello hello = applicationContext.getBean(IHello.class);
根据ServletContext,利用Spring的工具类:WebApplicationContextUtils
执行WebApplicationContextUtils.getWebApplicationContext(ServletContext)
拿到了ApplicationContext之后,即拿到了Spring的Bean容器,就可以通过容器获取Bean了,比如现在有一个Bean实现了IHello接口,那么可以通过以下代码获取这个IHello的bean实例: IHello hello = applicationContext.getBean(IHello.class);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在应用中一般普通的JavaPojo都是由spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是 Filter,一个是Servlet,这两样东西都是由Servlet容器来维护管理的,所以如果想和其他的Bean一样使用Autowire来注入的 话,是需要做一些额外的功夫的。
在servlet取得spring beans的方法:
<span style="font-size:24px;">package spring.servlets;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import spring.beans.User;
import spring.services.UserServices;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserServices userServices;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext 再获取 WebApplicationContextUtils
ServletContext servletContext = this.getServletContext();
WebApplicationContext context =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
userServices = (UserServices) context.getBean("userServices");
System.out.println("--------------------------");
User u = userServices.getUser();
System.out.println(u.getUserName());
System.out.println(u.getPassword());
System.out.println("--------------------------");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
</span>
在servlet取得spring beans的方法:
<span style="font-size:24px;">package spring.servlets;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import spring.beans.User;
import spring.services.UserServices;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserServices userServices;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext 再获取 WebApplicationContextUtils
ServletContext servletContext = this.getServletContext();
WebApplicationContext context =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
userServices = (UserServices) context.getBean("userServices");
System.out.println("--------------------------");
User u = userServices.getUser();
System.out.println(u.getUserName());
System.out.println(u.getPassword());
System.out.println("--------------------------");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
</span>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
servlet类中调用如下方法
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object beanName= context.getBean("beanId");
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object beanName= context.getBean("beanId");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询