Servlet不能自动注入Bean

来源:互联网 发布:淘宝直播怎么弄 编辑:程序博客网 时间:2024/06/03 18:25

Servlet不能自动注入Bean

ServletContextListener或HttpServlet的实现类不能主动注入Bean

ServletContextListener

@Overridepublic void contextInitialized(ServletContextEvent event) {    WebApplicationContextUtils        .getRequiredWebApplicationContext(event.getServletContext())        .getAutowireCapableBeanFactory().autowireBean(this);}

HttpServlet

@Overridepublic void init(ServletConfig config) throws ServletException {    WebApplicationContextUtils        .getRequiredWebApplicationContext(config.getServletContext())        .getAutowireCapableBeanFactory()        .autowireBean(this);}
0 0
原创粉丝点击