What does "bean X is injected into bean Y" mean in context of Spring framework
Seems,that very basic question.
Anyway can开发者_StackOverflow'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.
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.
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.
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
精彩评论