Depenedeny Injection In Spring

Depenedeny Injection In Spring

Dependency injecion(DI) is a process where by objects define their dependencies.That means of Dependendency Injection(DI) is a process of injecting the dependencies in to an object.

Advantages of Dependency Injection:

1.Dependency injection is design pattern so it will remove the dependency from the programming code.
2.The application development will become faster

Types Of Dependency Injection

1.Setter injection
2.constructor injection

1.Setter Injection:

Setter Injection is accomplished by Spring (IOC) container by calling The setter methods of beans.
The following example shows a POJo class that can only be dependency-injection using setter injection.
public class MovieNames{
//The MovieNames bean has dependency on the movieName
private String movieName;
//setter method
public void setMovieFind(MovieFind movieFind){
this.movieFind = movieFiind;
}
}

Now Configure bean in applicationContext.xml.Spring container will use the &ltproperty&gt tag to call The setter methods of bean to configure the setter based Dependency Injection.
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<beans>
<bean id="movienames" class="com.javalschool.MovieNames">
<property name = "movieName" value = "ABCD2"/>

</bean>
</beans>
We are injecting the movieName dependency into MovieNames Through Setter Method.


Constructor Injection:

Constructor Injection is accomplished by Spring(IOC) Container by Invoking the parameterized constructor of beans.
The following example shows a POJO class that can only be dependency-injection using constructor injection.
package com.javalschool;

public class Product {
private String productName;


public Product(String productName) {
super();
this.productName = productName;
}


Now Configure bean in applicationContext.xml.Spring container will use the &ltconstructor-arg&gt tag to call The parameterized constructor of bean to configure the constructor based Dependency Injection.
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<beans>
<bean id="product" class="com.javalschool.Product">
<constructor-arg value = "ABCD"/>

</bean>
</beans>
We are injecting the productName dependency into Product Through parameterized constructor.



-->