You can't. @Autowired
works only after context is initialized.
So you can do this hack:
public class MyListener implements ServletContextListener {
private MyBean myBean;
@Override
public void contextInitialized(ServletContextEvent event) {
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
this.myBean = (MyBean)ctx.getBean("myBean");
}
}
or better solution would be thx to Boris the Spider:
public class MyListener implements ServletContextListener {
@Autowired
private MyBean myBean;
@Override
public void contextInitialized(ServletContextEvent event) {
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
ctx.autowireBean(this);
}
}