Spring 的 bean 在应用程序上下文中运行。
Application Context 是 Spring 的高级容器。如同
BeanFactory,它可以加载bean定义,将bean连接在一起,以及
根据要求分配豆子。此外,它还添加了更多
企业特定的功能,例如解决问题的能力
来自属性文件的文本消息以及发布的能力
将事件应用程序发送给感兴趣的事件侦听器。这个容器是
由 org.springframework.context.ApplicationContext 接口定义。https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm
对于每个应用程序上下文,您可以有许多配置文件、配置类或两者的混合。
您可以使用如下代码创建应用程序上下文:
ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");
并获得豆子context.getBean
或与@autowired
.
在某些情况下,您希望(或需要)拥有上下文层次结构。在这些情况下,Spring 提供了一种指定父上下文的方法。如果您查看此构造函数,您将看到它接收一个上下文父级。
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext-org.springframework.context.ApplicationContext- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext-org.springframework.context.ApplicationContext-
正如您所看到的,父上下文与子上下文的类型相同,它们都是http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html.
不同之处在于它们通过父/子关系相关。不是组合(导入)关系。
最常见的情况是在 Spring MVC 应用程序中,该应用程序有 2 个上下文,第一个是调度程序 servlet 上下文,另一个是根上下文。
在这里你可以看到关系http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet
在这里您可以看到 Spring Boot 应用程序中应用程序上下文层次结构的示例。
https://dzone.com/articles/spring-boot-and-application-context-hierarchy https://dzone.com/articles/spring-boot-and-application-context-hierarchy