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:
Then i declared a generic email class implementing the email interfaceCode: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(); }
As expected, to construct my bean i used the following: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; } }
And my test class:Code:<bean id="email" class="classes.Generic"> <property name="to" value="christophorosp@gmail.com" /> <property name="from" value="test@test.com" /> <property name="subject" value="First Spring sample email" /> <property name="body" value="Welcome to the spring way of things" /> </bean>
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 theCode: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()); } }class section of the bean configuration xml file.Code:<bean id="email" class="classes.Generic">
Thanks


Reply With Quote