Reading:  

Quick to the point introduction to Spring Framework


Saying Hello World with Spring Framework

By using eclipse IDE creating applications of spring framework. The steps are:

  • create the java project
  • add spring jar files
  • create the class
  • create the xml file to provide the values
  • create the test class

Create java project:

Go to File, select New, and select Java Project. Give the name of the project and click Finish. Now the java project is created.

Add spring jar files

Right click on project and select build path, click Configure Build Path to add spring jars files and to configure the common logging API libraries. The spring jar files are

  • antlr-runtime-4.1.6
  • springframework.aop-4.1.6
  • springframework.asm-4.1.6
  • springframework.aspects-4.1.6
  • springframework.beans-4.1.6
  • springframework.context.support-4.1.6
  • springframework.context-4.1.6
  • springframework.core-4.1.6
  • springframework.expression-4.1.6
  • commons-logging-1.1.2

Create Java class:

To create the java class, Right click on 'src', click 'New' choose 'class'; specify the class name e.g. Customer and click 'finish'.

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    return cust_name;  
}  
  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
}  
  
public void displayMessage(){  
    System.out.println("Account holder name: "+cust_name);  
    }  
}  
 

Create the configuration xml file:

To create the configuration xml file click on 'src' select 'new' choose 'file' - give the file name as Bean.xml click 'finish'. You can name this configuration file anything you wish, for simplicity we chose to name is Bean.xml as the config is related to beans.

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
  <bean id="custbean" class="pack1.Customer">  
  <property name=" cust_name " value="Nimrit"></property>  
  </bean>    
</beans>  
 

 

Create the CusTest class:

Create the java class e.g. CusTest. Here we are getting the object of Customer class from the IOC container using the getBean() method of BeanFactory. Let's see the code of test class.

package pack1;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
  
public class CusTest {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("Bean.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  
      
    Customer customer=(Customer)factory.getBean("custbean");  
    customer.displayMessage();  
     }  
}  
 

Running the Program:

To run the program click on run oprtion or use shortcut key Ctrl+F11 to compile the program. Now run the ExmTest class we will get the output.

Account holder name: Nimrit

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!