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

Related Posts:

  • Spring Framework Architecture Spring framework is a layered architecture where Spring 2.x is divided into 6 modules. So here i am going to explain all 6 modules one by one. Spring Core Module: It provide the fundamental part of spring Framework lik… Read More
  • IoC Container In Spring IOC stands for the Inversion of Container. It is a container used in Spring core Framework. It is responsible for managing the states of the object i.e from its creation till its destruction. Before Spring framework, develo… Read More
  • Benefit of using Spring Framework Provide a good programming practice by decoupling the layers. If you are using spring in your project it decouples the layers and hence it's easy to write the test case for them. So it's easy from maintenance point of view.… Read More
  • Spring Implicit Wiring : byType In this type of auto-wiring container detect the bean instance with its data type and checks the data type of the bean instance running in the container with the bean type mentioned in the dependency bean. Some of the task … Read More
  • Alternatives of Open Session In View(OSIV) Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These … Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic