“bean X 被注入到 bean Y 中”是什么意思? Spring框架上下文中的意思
看来,这是一个非常基本的问题。
无论如何,无法从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这个问题非常基本。依赖注入是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,
When controller bean is created, the dependency will be resolved by the core container.
它基本上只是意味着您在应用程序上下文中创建了一个具有名称的 bean,并使用该 bean 的引用作为另一个 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.
In this example the bean with name bean1 gets injected into bean2. I don't think there is more to it.
它更像是一个协会。组合意味着 bean1 生命周期完全依赖于 bean2,但事实并非如此。可能有一个 id = "bean3" 的 bean 可能依赖于 bean 1。例如
,在本例中,bean3 也与 bean 1 关联,并且不管理 bean 1 的生命周期。
然而,Spring 支持使用内部 bean 进行组合。它本身不是组合,而是一个新的 bean 实例可以连接到另一个 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
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
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