|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test | SCWCD mock test | JSP mock test | ORACLE mock test | OCP mock test | Hibernate mock test | Servlet mock test | Struts mock test | EJB mock test | C mock test | C++ mock test | Aptitude mock test |Java online test | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test
***Java Code Examples*** |Technical Talk
Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
Student Live Projects >>> Tech Talk >>>
                                                                                                                                 
Tutorial Home
Struts Tutorial Home
Spring Tutorial Home
Hibernate Tutorial Home
JSP Tutorial Home
JDBC Tutorial Home
Webservices Tutorial Home
EJB Fundamentals

Hibernate

  1. Advantage of Hibernate over JDBC
  2. Hibernate Setup with an web Application
  3. First Hibernate Application
  4. Hibernate mapping with Database TABLE
  5. Hibernate Data Type-Java Data Type - SQL Data Type mapping
  6. One to Many Relation in Hibernate
  7. One to Many Relation in Hibernate bi-directional
  8. Many to Many Relation in Hibernate
  9. HQL: The Hibernate Query Language
  10. Criteria Queries
  11. Criteria Queries : Equal (eq), Not Equal(ne), Less than (le), greater than (gt),greater than or equal(ge) and Ordering the results
  12. Criteria Queries: And OR conditions
  13. Hibernate generator to generate id (primary key)
  14. prevent concurrent update in Hibernate,slate object updatation in Hibernate,version checking in Hibernate

    Struts


  1. Model View Controller (MVC)
  2. Model View Controller (MVC)
  3. Struts Flow-How Struts Works?
  4. Struts Tutorial - Struts Setup- First Struts Action class setup
  5. Message Resources
  6. Validation Framework
  7. Validation Framework-client side
  8. ForwardAction
  9. IncludeAction
  10. DispatchAction
  11. LookupDispatchAction
  12. DynaActionForm
  13. DynaActionForm
  14. Struts Tutorial - Mutli-click prevention using struts tokens-Prevent Duplicate Submission
  15. Logic Iterate Map and List

JSP


  1. JSP Tutorial
  2. Introduction to JSP
  3. JSP Comments
  4. JSP Syntax
  5. JSP Scripting Elements :Scriptlet, expression, declaration
  6. JSP Directives
  7. implicit objects in JSP
  8. JSP Actions
  9. Introduction to JSP
  10. jsp:useBean
  11. The jsp:setProperty Action
  12. The jsp:getProperty Action
  13. Introduction to JSP

Spring


  1. Spring Tutorial
  2. Introduction to Spring
  3. Benefits of Using Spring Framework
  4. Inversion of Control in Spring
  5. Introduction to BeanFactory
  6. Dependency Injection in Spring
  7. Collections Setter Injection
  8. Bean Scopes in Spring
  9. Spring IOC Setup Step by Step
  10. Bean Lifecycle in Spring
  11. ApplicationContext
  12. MessageSources in Spring
  13. Web Spring MVC framework
  14. Developing Your First Spring Web Application
  15. Developing Your Second Spring Web Application with Spring Form
  16. Developing Your First Spring Web Application with Spring Validation Framework with Code Example
  17. Spring integration with Hibernate

Spring Tutorial Step by Step


Spring Setup

Here is the steps how to setup spring IOC

Step 1. create the folders like mentioned below


Folder Structure

lib folder contains two jars (spring.jar and commons-logging.jar);

Step 2. The springexample-test.xml is the spring configuration file

springexample-test.xml contains all the informations related to beans objects with relations. ServiceIntegration class is the class which integrate the Email and SMS Servive in this Example. Put springexample-test.xml file into "spring" floder mentioned in the diagram.
ServiceIntegration class has two property emailInterface and smsInterface so you should have two setter and getter method in ServiceIntegration class.

<?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.

The information you are posting should be related to java and ORACLE technology. Not political. Your Ad Here