Skip to content

Instantly share code, notes, and snippets.

@rponte
Created May 19, 2014 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rponte/e082815d9a44154351ab to your computer and use it in GitHub Desktop.
Save rponte/e082815d9a44154351ab to your computer and use it in GitHub Desktop.
Accessing Spring bean from Servlet
public class AppServlet extends HttpServlet {
@Autowired
ApplicationContext applicationContext;
@Override
public void init(ServletConfig arg0) throws ServletException {
WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext())
.getAutowireCapableBeanFactory().autowireBean(this);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("execução iniciada em: " + new Date());
GerenciadorTasks gt = (GerenciadorTasks) this.applicationContext.getBean(SpringNameBeans.GERENCIADOR_TASKS);
gt.init();
out.println("execução terminada em: " + new Date());
}
}