English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring Examples

Here, we will learn the simple steps to create the first spring application. To run this application, we do not use any IDE. We just use the command prompt. Let's see the simple steps to create a spring application

Create a Java class Create an xml file to provide values Create the test class Load the spring jar file Run the test class


steps to create a spring application

Let's take a look at creating the first spring's5steps

1) create Java class

This is a simple Java bean class that only contains the name property.

package com.w3codebox;
public class Student {
private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public void displayInfo(){
    System.out.println("Hello: "+name);
}
}

This is a simple bean class that only contains a property name with its getter and setter methods. This class includes an additional method named displayInfo(), which prints the student's name through a greeting message.

2) create xml file

If you use the myeclipse IDE, you do not need to create an xml file because myeclipse can complete this operation itself. Open the applicationContext.xml file and write the following code:

<?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="studentbean" class="com.w3codebox.Student">
<property name="name" value="Vimal Jaiswal"></property>
</bean>
</beans>

bean The element is used to define a bean for a given class. The bean's property The child element specifies the property of the Student class named name. The value specified in the property element will be set in the Student class object by the IOC container.

3) Create test class

Create a Java class, for example, a test class. Here, we use the getBean() method of BeanFactory to obtain the object of the Student class from the IOC container. Let's take a look at the code of the test class.

package com.w3codebox;
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 Test {
public static void main(String[] args) {
    Resource resource = new ClassPathResource("applicationContext.xml");
    BeanFactory factory = new XmlBeanFactory(resource);
    
    Student student = (Student)factory.getBean("studentbean");
    student.displayInfo();
}
}

Resourceobject represents the information of applicationContext.xml file. Resource is an interface, and Resource is the implementation class of the Resource interface. Resource is an interface, and BeanFactory responsible for returning the Bean. XmlBeanFactory is the implementation class of BeanFactory. There are many methods in the BeanFactory interface. One method is getBean(), which returns the object of the associated class.

4) Load the jar files required for the spring framework

Running this application mainly requires three jar files.

org.springframework.core-3.0.1.RELEASE-A com.springsource.org.apache.commons.logging-1.1.1 org.springframework.beans-3.0.1.RELEASE-A

You can download the jar files required for the spring core application for future use.

Download the core jar file of Spring

Download all the spring jar files, including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm, etc.

To run this example, you just need to load the spring core jar file.


5) Run Test Class

Now run the Test class. You will get the output Hello: Vimal Jaiswal.