Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring interface injection example

No one so far was capable of providing a working correct example of interface injection in Spring Framework.

Martin Fowler article is not for mortals, everything else just words positioned in a very confusing way. I have surfed over THIRTY articles, where people either tell "Spring doesn't directly supports for interface injection"("and because I don't know exactly how I will only describe setter and constructor injections") or either "I will discuss it in my other threads" or either there will be few comments below saying that it is wrong example. I don't ask for explanation, I BEG for example.

There are three types of injection: Constructor, Setter and Interface. Spring doesn't support the latest directly(as I have observed people saying). So how is it done exactly?

Thank You,

like image 509
Aubergine Avatar asked Apr 20 '12 14:04

Aubergine


People also ask

Can we inject interface in Spring?

DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection. Also see Interface injection is not implemented in Spring clearly states it. So there are only two variants of DI. So if documentation says nothing about interface injection, its clear that its not there.

Can interface be injected?

Interface Injection is similar to Getter and Setter DI, the Getter, and Setter DI use default getter and setter but Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property.

What is Spring dependency injection explain with suitable example?

The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. The design principle of Inversion of Control emphasizes keeping the Java classes independent of each other and the container frees them from object creation and maintenance.


2 Answers

With interface injection an interface explicitly defines the point where a dependency can be set:

interface InjectPerson {     public void injectHere(Person p); }  class Company implements InjectPerson {    Person injectedPerson;      public void injectHere(Person p) {         this.injectedPerson = p;     } } 
like image 31
Stefan Avatar answered Sep 19 '22 17:09

Stefan


According to Variants of DI in spring

DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection.

Also see Interface injection is not implemented in Spring clearly states it.

So there are only two variants of DI. So if documentation says nothing about interface injection, its clear that its not there. Those who believe that interface injection is done by providing setter method in interface answer me:

  1. Why spring ref doc left mention of interface injection?
  2. Why can't interface injection via providing setter method NOT considered as setter injection itself. Why create special term for that when introduction of interface doesn't affect anything, I mean its still configured the same way. If they were different then how can one find it via seeing the config. Shouldn't it be transparent that in config and not seeing the impl that actually configured class implements some interface ?
  3. Just like Instantiation using an instance factory method and Instantiation using an static factory method, some bean attributes should clarify the interface injection?
like image 118
nanosoft Avatar answered Sep 16 '22 17:09

nanosoft