AnnotationConfigApplicationContext和AnnotationConfigEmbeddedWebApplicationContext

在以前的spring项目中,会有两个WebApplicationContext,一个是parent,从applicationContext.xml里加载的,一个是child,从servlet-context.xml里加载的。

两者是继承关系,child WebApplicationContext 可以通过getParent()函数获取到root WebApplicationContext。也就是说,我们常常通过listener初始化parent,并component-scan扫描非controller类。而通过servlet初始化child,只扫描controller类。这样便于在child可注入parent中的bean,反之就不行。

在spring boot中,好像只选择下面的其中之一:

1、AnnotationConfigApplicationContext是spring框架的多种应用上下文之一,位于spring-context.jar。使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文.避免使用application.xml进行配置。

2、AnnotationConfigEmbeddedWebApplicationContext是spring boot中的WebApplicationContext。

猜你喜欢

转载自1181731633.iteye.com/blog/2382809