English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Here, we will use the eclipse IDE to create a simple application of the spring framework. Let's see the simple steps to create spring applications in the Eclipse IDE.
Create Java project Add spring jar files Create a class Create xml files to provide values Create test class
Let's take a look at the steps to create the first spring application using the following steps5steps: eclipse IDE.
Go to FileMenu- New- Project- Java project. Enter the project name, for example firstspring- Complete. Now, the Java project has been created.
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 by the spring core application for future use.
Download the core jar file of Spring
Download all Spring jar files, including aop, mvc, j2ee, remoting, oxm, etc.
To run this example, you just need to load the spring core jar file.
To load jar files in the Eclipse IDE, Right-click on your project- Build path- Add external archive files- Select all necessary jar files- Complete..
In this case, we are simply creating a Student class with a name attribute. The student's name will be provided by the xml file. This is just a simple example, not the actual use of spring. We will see the actual usage in the "Dependency Injection" chapter. To create a Java class, please Right-click src - New- Class- Write the class name, for example, student- Complete. Write the following code:
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.
Create an xml file click src-New-file-Give the filename, for example, applicationContext.xml-Complete. 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 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.
Create a Java class, for example, a test. 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(); } }
Now run this class. You will get the output Hello: Vimal Jaiswal.