Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

spring Eclipse


Here, we are going to create a simple application of spring framework using eclipse IDE. Let’s see the simple steps to create the spring application in Eclipse IDE.

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

Steps to create spring application in Eclipse IDE

Let’s see the 5 steps to create the first spring application using eclipse IDE.

1) Create the Java Project

Go to File menu – NewprojectJava Project. Write the project name e.g. firstspring – Finish. Now the java project is created.

2) Add spring jar files

There are mainly three jar files required to run this application.

  • 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

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

download the core jar files for spring

download the all jar files for spring including aop, mvc, j2ee, remoting, oxm, etc.

To run this example, you need to load only spring core jar files.

To load the jar files in eclipse IDE, Right click on your project Build PathAdd external archivesselect all the required jar filesfinish..

3) Create Java class

In such case, we are simply creating the Student class have name property. The name of the student will be provided by the xml file. It is just a simple example not the actual use of spring. We will see the actual use in Dependency Injection chapter. To create the java class, Right click on srcNew classWrite the class name e.g. Studentfinish. Write the following code:

  1. package com.Kreationnext;
  2. public class Student {
  3. private String name;
  4. public String getName() {
  5.     return name;
  6. }
  7. public void setName(String name) {
  8.     this.name = name;
  9. }
  10. public void displayInfo(){
  11.     System.out.println(“Hello: “+name);
  12. }
  13. }

This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named displayInfo() that prints the student name by the hello message.

4) Create the xml file

To create the xml file click on src – new – file – give the file name such as applicationContext.xml – finish. Open the applicationContext.xml file, and write the following code:

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:p=“http://www.springframework.org/schema/p”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  7.                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
  8. <bean id=“studentbean” class=“com.Kreationnext.Student”>
  9. <property name=“name” value=“Vimal Jaiswal”></property>
  10. </bean>
  11. </beans>

The bean element is used to define the bean for the given class. The property subelement of bean 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.

5) Create the test class

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

  1. package com.Kreationnext;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.core.io.Resource;
  6. public class Test {
  7. public static void main(String[] args) {
  8.     Resource resource=new ClassPathResource(“applicationContext.xml”);
  9.     BeanFactory factory=new XmlBeanFactory(resource);
  10.     Student student=(Student)factory.getBean(“studentbean”);
  11.     student.displayInfo();
  12. }
  13. }

Now run this class. You will get the output Hello:

speclipse

Comments

comments