spring父上下文和子上下文之间有什么区别? [英] What is the difference between spring parent context and child context?

查看:164
本文介绍了spring父上下文和子上下文之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读spring doc核心容器我希望在注入协作者时理解 ref parent 的目的然后我发现了父上下文子上下文或父容器和当前容器的概念这是其中的一部分我很困惑:
这部分文档

I was reading spring doc the core container I want to understand the purpose of ref parent when inject collaborators then I found this notion of parent context child context or parent container and current container this is the part that I'm confuse about it : This part of doc


通过parent属性指定目标bean会创建
引用到当前
容器的父容器中的bean。 parent属性的值可以与
目标bean的id属性或目标bean的名称
属性中的值之一相同,并且目标bean必须在parent
当前容器的容器。你使用这个bean引用变量
主要是当你有一个容器层次结构,你想要在一个父容器中包装一个
现有的bean,其代理将具有与父代相同的
名称bean。

Specifying the target bean through the parent attribute creates a reference to a bean that is in a parent container of the current container. The value of the parent attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean, and the target bean must be in a parent container of the current one. You use this bean reference variant mainly when you have a hierarchy of containers and you want to wrap an existing bean in a parent container with a proxy that will have the same name as the parent 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>

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

can someone give me some help or an example of this two type of context ? and what is the purpose of the ref parent Thank you in advance

推荐答案

春天的豆子在应用程序上下文中运行。

The beans of the Spring run inside an application context.


应用程序上下文是Spring的高级容器。与
BeanFactory类似,它可以根据请求加载bean定义,连线bean和
分配bean。此外,它还增加了更多
企业特定功能,例如从属性文件解析
文本消息的能力,以及向感兴趣的事件监听器发布
应用程序事件的能力。此容器是org.springframework.context.ApplicationContext接口定义的

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 they both.

您可以使用以下代码创建应用程序上下文:

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

ApplicationContext context = new FileSystemXmlApplicationContext("Beans.xml");

并使用 context.getBean获取bean 或者 @autowired

在某些情况下,您希望(或需要)拥有上下文层次结构。在这种情况下,Spring提供了指定父上下文的方法。如果看看这个构造函数,你会看到它接收一个上下文的父。

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

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

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.

最常见的情况是你在Spring MVC应用程序中看到这个,这个应用程序有2个上下文,frist是调度程序servlet上下文,另一个是root上下文。
在这里你可以看到关系
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet

The most common case where you see this is in a Spring MVC application, this applications have 2 context, the frist 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

在这里,您可以看到Spring启动应用程序中应用程序上下文层次结构的示例。

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

这篇关于spring父上下文和子上下文之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆