“bean X 被注入到 bean Y 中”是什么意思? Spring框架上下文中的意思

发布于 2024-08-31 17:29:11 字数 165 浏览 7 评论 0原文

看来,这是一个非常基本的问题。
无论如何,无法从 Spring 教程中获得下一个定义的含义 - “bean X 被注入到 bean Y”

这是否意味着类之间的组合关系(当一个bean引用另一个bean时)?或者还有更多的东西?

感谢您的回答或参考与解释。

Seems,that very basic question.
Anyway can't get the meaning of next definition from Spring tutorials - "bean X is injected into bean Y" .

Does it mean composition relation between classes(when one bean has reference to another)? Or it's something more?

Thank you for answer or reference with explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-09-07 17:29:11

是的,这个问题非常基本。依赖注入是Spring框架的基本功能之一。 Java类应该尽可能独立,这增加了重用类和独立测试类的灵活性。对于这种解耦,一个类对另一个类的依赖关系应该由第三方注入到它们中,而不是类本身创建另一个对象。

在Spring框架中,一个称为Spring核心容器的轻量级容器执行依赖注入。即该容器将在所需对象中注入所需的依赖项。

在Web应用程序中,会有控制器类、服务类、dao类等。在控制器类中将引用服务类,在服务类中将引用dao类。使用 spring 时,可以使用 XML 或注释或 Java 配置来配置依赖项。

以控制器类(MyController.java)和服务类(MyService.java)之间的场景为例,

在xml配置文件中,我们定义依赖关系如下:




当控制器 bean 创建时,依赖关系将由核心容器解析。

Yes, The question is very basic. Dependency Injection is one of the fundamental functionality of Spring framework. Java classes should be independent as possible, this increases the flexibility to reuse the classes and testing classes independently. For this decoupling, the dependency of one class to another class should be injected into them by a third party rather than the class itself creates the other object.

In Spring framework, a light weight container called Spring core container performs dependency injection. ie this container will inject required dependencies in required objects.

In Web application, there will be a controller class, a service class, a dao class etc. In controller class there will be reference to service class, in service class there will be a reference to dao class. When using spring, the dependencies can be configured using XML or annotation or Java config.

Take the scenario between controller class (MyController.java) and service class (MyService.java),

In the xml configuration file, we define the dependency as follows,

<bean id="myService" class="com.service.MyService"/>
<bean id="myController" class="com.web.MyController">
<property name="myService" ref="myService">
</bean>

When controller bean is created, the dependency will be resolved by the core container.

尤怨 2024-09-07 17:29:11

它基本上只是意味着您在应用程序上下文中创建了一个具有名称的 bean,并使用该 bean 的引用作为另一个 bean 定义的属性值。

<bean name="bean1" class="com.example.A">
    <property name="property" value="hello" />
</bean> 
<bean id="bean2" class="com.example.B">
    <property name="aProperty" ref="bean1" />
    <property name="x" value="Test" />
</bean>

在此示例中,名称为 bean1 的 bean 被注入到 bean2 中。我认为没有更多的事情了。

It basically just means that you created a bean with a name in your application context and use the reference of that bean as a property value for another bean definition.

<bean name="bean1" class="com.example.A">
    <property name="property" value="hello" />
</bean> 
<bean id="bean2" class="com.example.B">
    <property name="aProperty" ref="bean1" />
    <property name="x" value="Test" />
</bean>

In this example the bean with name bean1 gets injected into bean2. I don't think there is more to it.

停顿的约定 2024-09-07 17:29:11

它更像是一个协会。组合意味着 bean1 生命周期完全依赖于 bean2,但事实并非如此。可能有一个 id = "bean3" 的 bean 可能依赖于 bean 1。例如

<bean id = "bean3" class = "com.foo.Foo">
  <property name = "aProperty" ref = "bean1" />
</bean>

,在本例中,bean3 也与 bean 1 关联,并且不管理 bean 1 的生命周期。
然而,Spring 支持使用内部 bean 进行组合。它本身不是组合,而是一个新的 bean 实例可以连接到另一个 bean 中。下面给出了一个示例。

<bean id = "bean3" class ="com.foo.Foo">
   <property name = "aProperty">
         <bean class = "com.bar.Bar">
            Wire dependencies here.
         </bean>
   </property>
</bean>

您不需要使用 id 属性来标识内部 bean,因为 Spring 容器会忽略它。有关内部 bean 的更多信息,此链接将非常有用。
http://static.springsource。 org/spring/docs/2.5.x/reference/beans.html#beans-inner-beans

It is more of an association. Composition implies that bean1 life cycle is completely dependent on bean2 which is not the case. There might be a bean with id = "bean3" that may depend on bean 1. For example

<bean id = "bean3" class = "com.foo.Foo">
  <property name = "aProperty" ref = "bean1" />
</bean>

In this case, bean3 is also associated to bean 1 and does not manage the life cycle of bean 1.
However, Spring supports composition using inner beans. It is not composition per se but a new instance of bean can be wired inside another bean. An example of that is given below

<bean id = "bean3" class ="com.foo.Foo">
   <property name = "aProperty">
         <bean class = "com.bar.Bar">
            Wire dependencies here.
         </bean>
   </property>
</bean>

You don't need to use id attribute to identify an inner bean as it will be ignored by Spring container. For more information on inner bean, this link will be more than useful.
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-inner-beans

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文