爲何servlet中沒法直接autowired bean from applicationC...

若是servlet中須要用到spring中的applicationContext,咱們是無法直接經過@Autowired方式獲取的,須要用以下方式: java

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對象。 spring

相關文章
相關標籤/搜索