spring父上下文和子上下文之间有什么区别?

E2r*_*abi 7 java spring spring-mvc

我正在阅读spring doc核心容器我希望在注入协作者时了解ref parent的目的然后我发现了父上下文子上下文或父容器和当前容器的概念这是我对它感到困惑的部分: 这部分博士

通过parent属性指定目标bean会创建对当前容器的父容器中的bean的引用.parent属性的值可以与目标bean的id属性相同,也可以与目标bean的name属性中的一个值相同,并且目标bean必须位于当前bean的父容器中.您主要在拥有容器层次结构并且希望将现有bean包装在父容器中并使用与父bean具有相同名称的代理时使用此bean引用变体.

<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
    <!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <!-- bean name is the same as the parent bean -->
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
        <ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
    </property>
    <!-- insert other configuration and dependencies as required here -->
</bean>
Run Code Online (Sandbox Code Playgroud)

有人可以给我一些帮助或这两种情境的例子吗?以及ref父母的目的是什么 ,请提前谢谢

reo*_*eos 7

Spring的bean在应用程序上下文中运行。

The Application Context is Spring's advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. Additionally, it adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by org.springframework.context.ApplicationContext interface. https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm

For each application context you can have many configuration files, configuration classes or a mix of both.

You can create an application context with a code like this:

ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");
Run Code Online (Sandbox Code Playgroud)

And obtain the beans with context.getBean or with @autowired.

There are some cases when you want(or need) to have a context hierarchy. In these cases Spring provides a way of specifying a parent context. If you look at this constructor, you will see that it receives a context parent.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext-org.springframework.context.ApplicationContext-

As you can see the parent context is the same type of a child context, they both are http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html.

The difference is that they are related through a parent/child relationship. Not a compose(import) relationship.

The most common case where you see this is in a Spring MVC application, this applications have 2 context, the first is the dispatcher servlet context and the other one is the root context. Here you can see the relationship http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet

And here you can see an example of application context hierarchy in spring boot applications.

https://dzone.com/articles/spring-boot-and-application-context-hierarchy