在 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"); }