Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 514 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring interface injection example

#1
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,
Reply

#2
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;
}
}
Reply

#3
I think someone answered your questions [here][1]
I also didn't know what Interface injection is until I read this statement from "Pro Spring MVC with web flow book"

"Note that interface-based dependency injection isn’t supported by the Spring Framework. This
means that we need to specify which concrete implementation to inject for a certain interface."


[1]:

[To see links please register here]

Reply

#4
There are 3 types of dependency injections:-

1. Constructor Injection(E.g Pico Container, Spring supports it).
2. Setter Injection(E.g Spring supports it).
3. Interface Injection(E.g Avalon, Spring does not support it).

Spring supports only constructor and setter based injection.
Looks like you got confused in the different types(3) and what spring supports(2 of them).

Reply

#5
Please check the below example for iterface injection.

There is a shape interface and 2 concrete classes which imiplements shape namely square and rectangle.

The interface


package di.interfaceinjection;
public interface Shape {
public String shapeName();
public void displayName();
}

2 Implemented classes

package di.interfaceinjection;

public class Square implements Shape {

@Override
public String shapeName() {
return "Square";
}

@Override
public void displayName() {
System.out.println("Square");
}

}

package di.interfaceinjection;

public class Rectangle implements Shape{

@Override
public String shapeName() {
return "Rectangle";
}

@Override
public void displayName() {
System.out.println("Rectangle");
}

}

Now, we have a class which sets the shape.


public class ShapeSetter {

private Shape shape;

public Shape getShape() {
return shape;
}

public void setShape(Shape shape) {
this.shape = shape;
}

}

and finally the configuration

<bean id="shape1" class="di.interfaceinjection.ShapeSetter">
<property name="shape" ref="square"></property>
</bean>
<bean id="shape2" class="di.interfaceinjection.ShapeSetter">
<property name="shape" ref="rectangle"></property>
</bean>
<bean id="square" class="di.interfaceinjection.Square"></bean>
<bean id="rectangle" class="di.interfaceinjection.Rectangle"></bean>


Here,

we are injecting different shapes.

package di.interfaceinjection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InterfaceInjeection {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("intro.xml");
ShapeSetter shape = (ShapeSetter)appContext.getBean("shape2");
shape.getShape().displayName();
}

}


Reply

#6
Hi I tried with a very simple approach that may clarify your answer.

following is the code that i have built on using two interfaces and and two bean classes.

first interface with name Job.

public interface Job {
public void setmyJob(String myJob);
public String getmyJob();
}
and one class to implement this interface with name as MyJob

public class MyJob implements Job {
public String myJob;

public MyJob() {
System.out.println("From MyJob default Constructor and the ID= "+this);
}

public void setmyJob(String myJob) {
this.myJob=myJob;
}

public String getmyJob() {
return myJob;
}
}


In the next step i created another Interface with name as Service


public interface Service {
public void setJob(Job job);
public Job getJob();
}


and then again another class to implement this Service Interface.


public class MyService implements Service {

public Job job;

public void setJob(Job job) {
this.job=job;
System.out.println("Hello from Myservice: Job ID="+job);
}

public Job getJob() {
return job;
}
}



then i created on main class with the main function and written the code as follows:


import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {

public static void main(String...a) {

BeanFactory beanfactory=new ClassPathXmlApplicationContext("Beans.xml");

MyService myservice=(MyService)beanfactory.getBean("myservice");
System.out.println("Before print");
System.out.println(myservice.getJob().getmyJob());
}
}



in my Beans.xml file i mentioned the code as follows and it worked.

<?xml version="1.0" encoding="UTF-8"?>
<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

[To see links please register here]

;


<bean id="myjob" class="MyJob">
<property name="myJob" value="My First String"/>
</bean>

<bean id="myservice" class="MyService">
<property name="job" ref="myjob"/>
</bean>
</beans>




I have also reffered to another online tutorials and then got this kind of solution. please let me know if you have any problem with this code. it is working for me.
Reply

#7
I think, that the confusion around interface injection is caused by missunderstanding what the term "interface injection" actually means. In my understanding, interface injection describes the ability of a bean contener to inject a new interface to the bean, no matter that the class definition of this bean is not implementing it.

All examples presented here show how to create a bean out of concrete class, and then how to inject it into another bean. The fact, that in all cases bean is injected into a field defined as an interface does not matter- all operations are done with beans created out of concrete instances.

I can provide also another catchy example:

package creditCards;

interface PaymentCard {
Boolean isDebitAllowed();
}

<bean id="card" class="creditCards.PaymentCard">
<lookup-method name="isDebitAllowed" bean="boolValue"/>
</bean>

<bean id="boolValue" class="java.lang.Boolean">
<constructor-arg type="boolean" value="true"/>
</bean>

As you see here, it is even possible to create a bean out of interface! Still, it is not a interface injection, as IoC contener initializes instanse of this bean by its own. In other words, `card` bean is an initialized object, not an interface, what makes, that the selected answer for this question is correct.
Reply

#8
According to [Variants of DI in spring][1]

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

Also see [Interface injection is not implemented in Spring][2] 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][3] and [Instantiation using an static factory method][4], some bean attributes should clarify the interface injection?

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through