According to The IoC Container, Spring can manage a context hierarchy or hierarchy of containers and then use the <ref parent="beanId"/>
to refer to a bean in a parent context. What mechanism does Spring use to create this container hierarchy? Can one use the <import resource="application-context.xml"/>
command to create this hierarchy? Please provide an example of an application context that forms a hierarchy.
问问题
9598 次
1 回答
15
应用程序上下文层次结构是自动创建的;例如,每个 Spring MVC 应用程序为每个 DispatcherServlet 创建单独的上下文。此上下文是公共父上下文的子上下文。这样,每个子上下文都可以从父上下文访问 bean,但反之则不行。同级上下文也相互分离且不可见。
您可以手动创建上下文层次结构,以在项目中提供更精细的粒度级别。这可以例如使用各种构造函数来实现ClassPathXmlApplicationContext
。
<import>
构造合并来自导入文件的 bean,因此它是一种将 bean 定义物理划分为多个文件的方法,但它们最终都在一个上下文中。顺便说一句,很遗憾没有 XML 标记来定义子上下文文件(?)
于 2011-03-14T18:19:59.323 回答