欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在 Servlet中获取Spring Bean servletspring 

程序员文章站 2024-03-21 20:29:22
...

 

在 Servlet中获取Spring Bean,备忘 不解释

 

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class HeartBeatServlet extends HttpServlet  {

	private static final long serialVersionUID = 6165817221166007344L;
	
	private static Logger log = LoggerFactory.getLogger(HeartBeatServlet.class);

	private IHeartBeatBolt hbBolt;
	
	
	@Override
	public void init() throws ServletException {
		/*
		 * How do I get a spring's bean from a servlet ? 
		 * REFER: http://www.kodejava.org/examples/32.html
		 * */
		ServletContext context = getServletContext();
		WebApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(context);
		hbBolt = (IHeartBeatBolt)applicationContext.getBean("hbBolt");
		log.info("HeartBeatServlet init ok");
	}
 
相关标签: servlet spring