Announcement Announcement Module
Collapse
No announcement yet.
Beginners Question: Spring, DI, Loose coupling and Interfaces Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Beginners Question: Spring, DI, Loose coupling and Interfaces

    Dear all, im in great need to understand the concept between Spring's DI and loose coupling with interfaces. I was reading through "Spring in Action vol3" book and i noticed that the author uses Interfaces to load the application context and load the beans. So i followed his example and started writing my own Spring application which is basically an email composer.
    In my application i declared an Email interface with the relevant setters and getters:
    Code:
    public interface Email {
    	//Setters
    	public void setTo(String To);
    	public void setFrom(String From);
    	public void setSubject(String subject);
    	public void setBody(String body);
    	
    	//Getters
    	public String getTo();
    	public String getSubject();
    	public String getBody();
    	public String getFrom();
    }
    Then i declared a generic email class implementing the email interface

    Code:
    package classes;
    
    import interfaces.Email;
    
    public class Generic implements Email {
    
    	private String To, From, subject, body;
    
    	public Generic() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public Generic(String To, String From, String subject, String body) {
    		this.To = To;
    		this.From = From;
    		this.subject = subject;
    		this.body = body;
    	}
    
    	@Override
    	public void setTo(String To) {
    		this.To = To;
    	}
    
    	@Override
    	public void setFrom(String From) {
    		this.From = From;
    	}
    
    	@Override
    	public void setSubject(String subject) {
    		this.subject = subject;
    	}
    
    	@Override
    	public void setBody(String body) {
    		this.body = body;
    	}
    
    	@Override
    	public String getTo() {
    		// TODO Auto-generated method stub
    		return To;
    	}
    
    	@Override
    	public String getSubject() {
    		// TODO Auto-generated method stub
    		return subject;
    	}
    
    	@Override
    	public String getBody() {
    		// TODO Auto-generated method stub
    		return body;
    	}
    
    	@Override
    	public String getFrom() {
    		// TODO Auto-generated method stub
    		return From;
    	}
    
    }
    As expected, to construct my bean i used the following:
    Code:
    	<bean id="email" class="classes.Generic">
    		<property name="to" value="[email protected]" />
    		<property name="from" value="[email protected]" />
    		<property name="subject" value="First Spring sample email" />
    		<property name="body" value="Welcome to the spring way of things" />
    	</bean>
    And my test class:
    Code:
    package test;
    
    import interfaces.Email;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    		Email email = context.getBean("email", Email.class);
    		System.out.println(email.getTo());
    	}
    
    }
    Now everything works great so far. However what confuses me is when i wish to swap the email bean class and use something similar with the above implementation (via interfaces) i can only use methods declared in the Interface. What if i, for example create another class that also implemetns the email interface but has additional methods to it. How can i then use spring to easily swap the class? Obviously it wont work when trying to call the new method NOT implemented in the template. So what im potentially asking is how is Spring making my life easier by eliminating the need to make many changes in the code? If i were to use the particular class, i would need to change all class declarations within my application and not just swap the
    Code:
    <bean id="email" class="classes.Generic">
    class section of the bean configuration xml file.

    Thanks
    Last edited by christophorosp; Nov 10th, 2011, 08:21 AM.

  • #2
    In your example of the second class, if we take Spring out of the equation. How does your code look in terms of coding to an interface. Coding to an interface works great if you make the public client calling methods of a class be part of the interface. In your example of the second class that has additional methods. Are they part of another interface that that class also implements? How would you do that and handle that in straight Java code without Spring. You would still have those same issues that you think you have.

    So that all really comes down to design and clean OO best practices and really not specific to Spring.

    Now just because that class implements an interface and normally we want to code to interfaces even with Spring, doesn't mean that you can't still get that instance out of Spring and then just reference it by whatever type you want of that class, either any interface that it implements or by the class type. So you would still have access to those other methods.

    I hope that was clear.

    On another note. Typically, you won't put domain objects, or better said objects that are holding state as Spring beans. You might instead have an EmailService class as a Spring bean, but the domain stateful object of Email usually wouldn't be. And as such the EmailService class would implement an interface because you only want clients of the service to be able to call only certain public facing methods.

    Mark

    Comment


    • #3
      I think you need clarification on the meaning of "Program to an Interface, not an implemention" principle. Given below are some examples that will clarify what this principle mean

      http://stackoverflow.com/questions/3...o-an-interface
      http://download.oracle.com/javase/tu...faces/set.html
      http://www.artima.com/lejava/article...rinciples.html

      Comment


      • #4
        Thank you both of you. I had an epiphany after reading both of your replies and the explanation/links you provided.
        Very, very helpful.
        Have a nice day.
        Last edited by christophorosp; Nov 11th, 2011, 03:07 AM.

        Comment

        Working...
        X