Skip to main content

Dependency Injection Definition

What is dependency management in Spring?

Lets consider the following interfaces and concrete class that implements interface.

public interface IOutputGenerator
{
 public void generateOutput();
}
 
public class CsvOutputGenerator implements IOutputGenerator
{
 public void generateOutput(){
  System.out.println("Csv Output Generator");
 }
}
public class JsonOutputGenerator implements IOutputGenerator
{
 public void generateOutput(){
  System.out.println("Json Output Generator");
 }
}
 
Lets call it directly.   
public class App
{
    public static void main( String[] args )
    {
     IOutputGenerator output = new CsvOutputGenerator();
     output.generateOutput();
    }
}
 
In this way, the problem is the “output” is coupled tightly to 
CsvOutputGenerator, every change of output generator may involve code 
change. If this code is scattered all over of your project, every change
 of the output generator will make you suffer seriously.  

Lets consider a spring implementation:
public class OutputHelper
{
 IOutputGenerator outputGenerator;

 public void generateOutput(){
  outputGenerator.generateOutput();
 }

 public void setOutputGenerator(IOutputGenerator outputGenerator){
  this.outputGenerator = outputGenerator;
 }
}
 
<!-- Spring-Common.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="OutputHelper" class="com.subash.output.OutputHelper">
  <property name="outputGenerator" ref="CsvOutputGenerator" />
 </bean>

 <bean id="CsvOutputGenerator" class="com.subash.output.impl.CsvOutputGenerator" />
 <bean id="JsonOutputGenerator" class="com.subash.output.impl.JsonOutputGenerator" />

</beans>
 
public class App
{
    public static void main( String[] args )
    {
     ApplicationContext context =
        new ClassPathXmlApplicationContext("Spring-Common.xml");

     OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
     output.generateOutput();

    }
}  
 Now, you just need to change the Spring XML file for a different output generator. When output changed, you need to modify the Spring XML file only, no code changed, means less error.With Spring framework – Dependency Injection (DI) is a useful feature for object dependencies management, it is just elegant, highly flexible and facilitates maintainability, especially in large Java project.

Ref:
http://www.mkyong.com/spring/spring-loosely-coupled-example/


Comments

Popular posts from this blog

ifference between @RestController and @Controller

Difference between @RestController and @Controller Annotation in Spring MVC and REST Read more:  http://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html#ixzz4sbGy2Glh The  @RestController  annotation in Spring MVC is nothing but a combination of  @Controller  and  @ResponseBody  annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the  REST web services  you know that the fundamental difference between a web application and a REST API is that the response from a web application is generally view (HTML + CSS + JavaScript) while REST API just return data in form of JSON or XML. This difference is also obvious in the  @Controller  and  @RestController  annotation. The job of  @Controller  is to create a Map of model object and find a view but  @RestContr...

Presentation topics

Topics for presentation : Knockout.js, Angular.js, Node,js, Kendo UI, MVC 4, Nuget, Ninject, Unity Framework, Dependency Injection , PMC Package Manager Control, Mordenizer, Moq, Json, Html5, Entity Framework code1st. Spring.net Castle Windsor , Structure Map and Microsoft Unity ,
What happens when you compile/run the following code: class MyClass { public static void main(String[] args) { new MyClass(); } Ans: It executes and create a object for that class without reference. The output of the program is nothing.