๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring First Program using XML Based Configuration


Steps to Create Spring First Program

  1. Open any IDE & Create New Project
  2. Add Spring Jar Files (or Dependencies if using MAVEN)
  3. Create POJO Class i.e. Student
  4. Create "XML Configuration File"
  5. Create Main Class, Start the Container & Access the Beans
1. Open any IDE & Create New Project
  • Open any IDE like Eclipse, IntelliJ, Spring Tool Suit (STS) etc and create new project as shown below.

  • Note : In our case we are using Eclipse IDE.







2. Add Spring Jar Files (or Dependencies if using MAVEN)
  • Add Spring Jar Files (If you are creating simple Java Project) OR Add Spring Dependency (If you are creating MAVEN Project)

  • Note : Here we are taken simple Java Project, MAVEN will be explained in further tutorials.

  • Required Jar Files are as below
    1. spring-core-x.x.x.jar
    2. spring-beans-x.x.x.jar
    3. spring-context-x.x.x.jar
    4. spring-expression-x.x.x.jar
    5. commons-logging-x.x.jar
    Where xxx is the version of jar file







3. Create POJO Class i.e. Student
  • Create one POJO class i.e. "Student" in package "in.sp.beans" and create one display() method to print Student details.





  • Student.java
    package in.sp.beans;
    
    public class Student
    {
    	private String name;
    	private int rollno;
    	private String emailid;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getRollno() {
    		return rollno;
    	}
    	public void setRollno(int rollno) {
    		this.rollno = rollno;
    	}
    	public String getEmailid() {
    		return emailid;
    	}
    	public void setEmailid(String emailid) {
    		this.emailid = emailid;
    	}
    
        public void display()
    	{
    		System.out.println("Name : "+name);
    		System.out.println("Roll No : "+rollno);
    		System.out.println("Email Id : "+emailid);
    	}
    }
4. Create "XML Configuration File"
  • Now click on src folder and create one package i.e. "in.sp.resources". Then right click on package and create new XML file named as "applicationContext.xml"



  • Note : "applicationContext.xml" is the standard name and its suggestable to use this name only (but we can change this name)

  • Note : Here we are providing the configurations using XML file, Click Here for Spring Program using "Java Configuration File".

  • Now, open google, search for "Spring Configuration File XML Schema", then open website and copy - paste the XML Schema as below :-



  • Then create Java Bean Object using <bean> tag and set the properties in Bean Object using <property> tag
  • Below is the full code of XML Configuration File
  • applicationContext.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean class="in.sp.beans.Student" id="stdId">
            <property name="name" value="Deepak" />
            <property name="rollno" value="101" />
            <property name="emailid" value="deepak@gmail.com" />
        </bean>
    
    </beans>
    
  • Spring Container will read the above XML Configuration file and will create one Student class bean object with properties name, rollno and email with respective values.
5. Create Main Class, Start the Container & Access the Beans
  • Right click on src folder and create new package i.e. "in.sp.main".
  • Right click on package i.e. "in.sp.main" and create new Java Class named as "MainApp.java".





  • Now create main method.
  • Next create Spring Container (We will use ApplicationContext as this is new Spring Container) and load the Spring Configuration File (XML file) with proper package name
    ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
  • Access the Java Bean Object and call the business method i.e. display().
  • Below is the full code :
    MainApp.java
    package in.sp.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import in.sp.beans.Student;
    
    public class MainApp
    {
    	public static void main(String[] args)
    	{
            //Spring container is created
    	    ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
    	    
            //Bean object is requested from Spring Container and stored in Student reference
    	    Student stdBean = (Student) context.getBean("stdId");
    
    	    stdBean.display();
    	}
    }
  • Now right click on the project or MainApp.java class and run the program, we will get the output as shown below :



  • Hurrey, Program completed and we got the output...!!