<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="service" class="springexample.service.ServiceIntegration"> <property name="emailInterface"> <ref bean="email" /> </property> <property name="smsInterface"> <ref bean="sms" /> </property> </bean>
<bean id="email" class="springexample.email.Email"> <property name="smtpHost"> <value>localhost</value> </property> <property name="fromEmail"> <value>admin@techfaq360.com</value> </property> <property name="userId"> <value>myuserid</value> </property> <property name="password"> <value>mypassword</value> </property> </bean> <bean id="sms" class="springexample.sms.SMS"> <property name="cellNumber"> <value>8645378290134</value> </property> </bean> </beans>
|
Step 3. ServiceIntegration class
Place the ServiceIntegration.java to service folder mentioned in the diagram.
ServiceIntegration class has two property emailInterface and smsInterface so you should have two setter and getter method in ServiceIntegration class. This means emailInterface and smsInterface objects are composition relation with ServiceIntegration. Here we don't directly create object. Spring IOC container read the springexample-test.xml file and create object for us. We have to use the getter method to access the objects.
Like : ServiceIntegration.getEmailInterface() will returm Email Object.
The Java Code is (ServiceIntegration.java)
package springexample.service;
import springexample.email.EmailInterface;
import springexample.sms.SMSInterface;
public class ServiceIntegration implements ServiceIntegrationInterface{ private EmailInterface emailInterface; private SMSInterface smsInterface;
/** * @return Returns the emailInterface. */ public EmailInterface getEmailInterface() { return emailInterface; } /** * @param emailInterface The emailInterface to set. */ public void setEmailInterface(EmailInterface emailInterface) { this.emailInterface = emailInterface; } /** * @return Returns the smsInterface. */ public SMSInterface getSmsInterface() { return smsInterface; } /** * @param smsInterface The smsInterface to set. */ public void setSmsInterface(SMSInterface smsInterface) { this.smsInterface = smsInterface; } public void startService(){ if(getEmailInterface() != null){ getEmailInterface().sendEmail(); System.out.println("Email sent to: "+getEmailInterface().getFromEmail()); } if(getSmsInterface() != null){ getSmsInterface().sendSMS(); System.out.println("SMS sent to : "+getSmsInterface().getCellNumber()); } } }
|
The Java Code is (ServiceIntegrationInterface.java)
package springexample.service;
import springexample.email.EmailInterface; import springexample.sms.SMSInterface;
public interface ServiceIntegrationInterface { public EmailInterface getEmailInterface();
public void setEmailInterface(EmailInterface emailInterface); public SMSInterface getSmsInterface();
public void setSmsInterface(SMSInterface smsInterface); public void startService();
}
|
Step 3. Email class and SMS Class
Both the class are offering services and ServiceIntegration class call both the service via IOC container. ServiceIntegration not creating objects of Email class and SMS Class . Spring IOC create object for ServiceIntegration.
In the Spring IOC we can add some properties with the class also .
For Example : Email class need smtpHost for sending email. You can see the springexample-test.xml where Email class is associated with smtpHost properties. So we can get hard coded constant value from XML also. Those values are set when IOC create object for the class.
The Java Code is (Email.java)
package springexample.email;
public class Email implements EmailInterface {
private String smtpHost; private String userId; private String password; private String fromEmail;
public void sendEmail(){ System.out.println("This is just Dummy Method for Testing Spring"); }
public String getFromEmail() { return fromEmail; }
public void setFromEmail(String fromEmail) { this.fromEmail = fromEmail; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getSmtpHost() { return smtpHost; }
public void setSmtpHost(String smtpHost) { this.smtpHost = smtpHost; } public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; } }
|
The Java Code is (EmailInterface.java)
package springexample.email;
public interface EmailInterface {
public void sendEmail();
public String getFromEmail();
public void setFromEmail(String fromEmail) ;
public String getPassword();
public void setPassword(String password) ;
public String getSmtpHost() ;
public void setSmtpHost(String smtpHost);
public String getUserId() ;
public void setUserId(String userId); }
|
The Java Code is (SMS.java)
package springexample.sms;
public class SMS implements SMSInterface { private String cellNumber; public void sendSMS(){ System.out.println("This is just SMS dummy method for testing Spring" ); }
/** * @return Returns the cellNumber. */ public String getCellNumber() { return cellNumber; } /** * @param cellNumber The cellNumber to set. */ public void setCellNumber(String cellNumber) { this.cellNumber = cellNumber; } }
|
The Java Code is (SMSInterface.java)
package springexample.sms;
public interface SMSInterface { public void sendSMS(); public String getCellNumber(); public void setCellNumber(String cellNumber);
}
|
Step 4. Test Client
Test Client from where we will get the ServiceIntegration object and start the service.
ServiceIntegrationInterface si = (ServiceIntegrationInterface)appContext.getBean("service");
si.startService();
Form command promt you can run the TestClient.java and test the spring IOC.
The Java Code is (TestClient.java)
package springexample.client;
import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;
import springexample.service.ServiceIntegrationInterface;
public class TestClient { public static void main(String[] args){ try { System.out.println("Example started"); XmlBeanFactory appContext = new XmlBeanFactory(new ClassPathResource("springexample-test.xml")); ServiceIntegrationInterface si = (ServiceIntegrationInterface)appContext.getBean("service"); si.startService(); } catch(Exception e){ e.printStackTrace(); } }
}
|
Click here to get the tutorial code : SpringProject.zip
Steps to run the downloded code :
step 1. set ant in your machine
step 2. unzip the SpringProject.zip file to D:\installable\SpringProject directory.
step 3. Go to command prompt -- d:\testSpring\SpringProject
step 4. run the command : ant -Daction=run
You can do like below and see the Spring Magic.
