Dependencies means how a java bean is dependent on another bean class. That means if we defines any bean reference as a class member to another bean class, then it results in a bean dependent on another. A primitive type or any reference type ( other than a bean reference ) involves no dependencies. Dependencies is only with a bean class references because to inject dependencies we need that bean instance. So this makes the dependency on another bean.
Important points regarding dependency injection
1. Dependency should only be injected if instance of that bean is available in the container.
2. Instance will be created as all the beans are mapped to the XML file.
3. Instance will be created for the current bean and dependency will be injected for the dependent bean.
4. While injecting the dependencies Container need its instance , if not available then try to find the mapped bean of the injecting bean and try to create the instance.
5. Instance creation of the current bean depend on the type of injection i.e on constructor or setter injection.
6. If both the instance for dependency for each other then it is called as Cyclic Dependency.
Basically there are two types of Dependencies Injection :
1. Constructor Injection
2. Setter Injection
Constructor Injection
Using constructor injection container is injecting the dependencies through constructor, for which instance must be available at the container. If not available then container search for its bean tag and then try to create an instance for that bean.
In the case of constructor injection
a. Container first create the instance of the current bean.
b. Then try to inject the dependencies.
Constructor Injection should be mapped in the XML file under the bean tag using the < constructor-arg > tag with " ref " attributes.
Setter Injection
With setter injection container is injecting the dependencies using the setter methods. It is same as the constructor injection with a difference in the order of creating the current bean instance.
In the case of Setter Injection
a. Container first try to inject the dependencies.
b. Then try to create the instance of current bean.
Setter Injection should be mapped in XML file under the bean tag using the < property > tag with the " ref " attribute.
Note : " ref " attribute contains the value of bean id which we have assigned while defining bean in XML. Explicitly writing the dependencies in the XML file is called as Explicit Wiring .
To know about Explicit wiring click here
0 comments:
Post a Comment