Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it right to use spring DI without interface

I am using spring dependency injection,where i can inject object dependency through some external xml file.

My question is :

Is it fine to use spring DI without using interfaces?

Because with DI,we want to achieve one thing :

If a class is being replaced by some other class having same methods but different definitions,then we don't need to make any change in code where this class is being referenced.

This is fine if i am using interface as interface can point any class which is implementing this interface but if i am directly injecting class object through DI then there is no meaning of DI because in this case if class get replaced,i have to change my code also where it is being referenced.

Please let me correct if something is wrong.

Let's say i have

Class Datasource{

    String url;
    String user;
    String database;

}

Now i am using it without DI as

Class Abc{

     Datasource datasource = new Datasource();
}

what's the problem in this and what are the benefits i can get if i use DI.

Is getting singleton object only goal of DI?

like image 752
Ankit Gupta Avatar asked Oct 09 '14 10:10

Ankit Gupta


People also ask

Do I really need to create interfaces in Spring?

So, it is not mandatory to create interfaces for Service, DAO but it is preferred if you are working on a fairly medium code base where there are multiple developers and possibly clients consuming those APIs outside of the application as well.

Is interface necessary for dependency injection?

But most often, it's better to introduce an interface to remove the dependency between the client and the service implementation. The injector is the only role that isn't required by the dependency inversion principle. But that's not an issue because you don't need to implement it.

Why do we need interface in Spring?

Using Interfaces allows your classes to extend from some other classes if required. Your Interfaces can have multiple implementations and you can switch between any of them without changing the client code.

What are the benefits of using Spring Framework no need for interfaces?

One of the major benefits of Spring Framework for development of enterprise application is that you can leverage from Spring. Spring uses technologies such as JDK timers, ORM frameworks, Java EE etc. So that developers need not have to learn all those technologies or frameworks in order to develop applications.


1 Answers

Dependency Injection isn't about interfaces or classes or enums or... It is about Inversion of Control.

Imagine the following class.

public class PersonService {

    private PersonRepository repository = new PersonRepository();
}

Apparently there is nothing wrong with this. However what if PersonRepository needs other dependencies, what if it takes another complex object as construct argument. Suddenly the PersonService is burdend with the logic on how to construct an object and all of its dependencies. Whereas it only wants to use the object.

public class PersonService {

    private PersonRepository repository;

    public PersonService() {
         InitialContext ctx = new InitialContext();
         repository = ctx.lookup("java:comp/env/repositories/person");
    }
}

The code above is tied to JNDI, how are you going to test it (easily) of course you can construct your own Mock JNDI service and pre configure that with a constructed or mocked repository but that is quite cumbersome.

 public class PersonService {

      private final PersonRepository repository;
      public PersonService(PersonRepository repository) {
          this.repository=repository;
      }
 }

The above makes basically everything possible, there is no burder on the PersonService on how to construct the PersonRepository it is just handed one, where it comes from it doesn't matter. Is it the actual class or a (class-based) proxy, is doesn't care.

Hence dependency injection, you want to hand the PersonRepository to use to the PersonService it shouldn't matter to it where it comes from, how it is constructed or if it is a proxy to an actual object. It just needs a PersonRepository.

like image 56
M. Deinum Avatar answered Oct 03 '22 06:10

M. Deinum