为什么servlet中无法直接autowired

来源:互联网 发布:外汇交易软件mt4 编辑:程序博客网 时间:2024/05/12 18:16
如果servlet中需要用到spring中的applicationContext,我们是没法直接通过@Autowired方式获取的,需要用如下方式:
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
 
running = context.getBean("running");
为什么呢?
在tomcat中,一个url请求进来到处理,经过这些步骤:tomcat 启动,tomcat comtxt加载(在这儿是spring comtext加载,因为spring的comtext实现了tomcat中容器接口),filters,servletcontext启动。。。在这儿servlet和applicationContext是两个平级的概念。所以servlet只能够通过tomcat中的api来获取applicationContext对象。
0 0
原创粉丝点击