Tuesday, January 12, 2010

Interface injection using Spring

I am adding one more sample for Interface Injection using spring. Spring directly does not support interface injection but there is one thing called autowire, that can be used in spring to achieve some sort of interface injection.

Consider below sample Java classes:


1. Text.java - interface which has getter and setter for String object

public interface Text {
void setTextString(String textString);
String getTextString();
}

2. MyText.java - implements above Text interface

public class MyText implements Text {

private String textString;

public String getTextString() {
return textString;
}

public void setTextString(String textString) {
this.textString = textString;
}

}

3. Service.java - which has getter and setter for above Text object

public interface Service {
void setText(Text text);
Text getText();
}

4. MyService.java - implements above Service Interface

public class MyService implements Service {
private Text text;

public Text getText() {
return text;
}

public void setText(Text text) {
this.text = text;
}
}

5. MyServiceApp.java - which has main method to test interface injection

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import static java.lang.System.out;


public class MyServiceApp {

public static void main(String...strings) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"myservice.xml"));
MyService myService = (MyService) beanFactory.getBean("myService");
out.println( myService.getText().getTextString());
}
}


Now, in above sample to get interface injection major file is configuration file(myservice.xml).

Look at below configuration


Configuration file: myservice.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<bean id="text" class="MyText" p:textString="My First String...!"/>

<!-- <bean id="myService" class="MyService" p:text-ref="text"/> -->
<!-- The above can be used if you want to inject the dependency yourself-->

<!-- The below can be used to leave the dependency injection decision to the IoC container -->
<!-- <bean id="myService" class="MyService" autowire="byName"/> -->

<!-- The following line is also almost same as the above except for this case IoC will
detect dependency by type -->
<bean id="myService" class="MyService" autowire="byType"/>

</beans>


In above sample you can configure interface injection by two ways:

1. <bean id="myService" class="MyService" p:text-ref="text"/>

Here, injection is done by yourself by giving reference to another bean object.

2. <bean id="myService" class="MyService" autowire="byName"/>

Here, injection decision is taken by IoC container as you have specified autowire attribute. autowire value can be set as byName or byType.

6 comments:

  1. Here Service is the interface and MyService is the class. I don't think this is correct.

    ReplyDelete
    Replies
    1. its correct because MyService is the class who's interface implementation defined in service layer(specifically data in some other system). And MyText class can be of persistence Layer which can be from another interface also.

      Delete
  2. I think this is not correct. As anonymous said, you are still injecting the class not the interface.

    ReplyDelete
  3. Spring doesn't have interface injection.

    ReplyDelete
  4. Ya using Aware interface we can achieve interface injection in spring....

    ReplyDelete