In this tutorial, i show you how to create spring application in Eclipse. It’s a simple application of spring framework and help you have a first look about this powerful framework.
Firstly, you should have Eclipse IDE on local computer if you have not yet, please download the lastest version from webapge eclipse.org
To create spring application in Eclipse you should follow to these below steps:
1. Create the Java Project
Now you open Eclipse IDE and go to File menu -> New -> project -> Java Project. Then you should type the project name (for example: springsample) and click Finish button. Now, the springsample project is already created.
2. You should add spring jar files into project
This is simple spring application so you only need five jar files below:
spring-core-4.2.6.RELEASE.jar
spring-beans-4.2.6.RELEASE.jar
spring-context-4.2.6.RELEASE.jar
spring-expression-4.2.6.RELEASE.jar
commons-logging-1.2.jar
To import the jar files in eclipse IDE, right click on your project -> Build Path -> Add external archives -> select all the above jar files in a directory -> finish.
3. Create Java class
In this tutorial, you are creating the Fruit class have name and produceby property. To create the java class, on the project explorer, right click on src -> New -> class -> Type the class name Fruit -> finish. Simply you implement this class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javabycode.spring; public class Fruit { private String name; private String produceby; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setProduceby(String produceby) { this.produceby = produceby; } public String getProduceby() { return produceby; } } |
4. Create the application context xml file for spring framework application
It’s easy to create the xml file by clicking on src -> New -> file -> type file name such as applicationContext.xml -> finish. You should configure a bean for the class Fruit as below:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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-4.0.xsd"> <bean id="fruit" class="com.javabycode.spring.Fruit"> <property name="name" value="Vimal Jaiswal"></property> <property name="produceby" value="Vimal Jaiswal"></property> </bean> </beans> |
This is a way to define the bean for the Fruit class. The values of name and produceby properties will be assigned for properties of the Fruit class by the IOC container.
5. Create the SpringSample class
In this SpringSample class you are getting the object of Fruit class from Spring IOC container using the getBean() method of ApplicationContext.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javabycode.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringSample { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Fruit fruit = (Fruit) context.getBean("fruit"); System.out.println("********** Display fruit bean **********"); System.out.println("name = " + fruit.getName()); System.out.println("produceby = " + fruit.getProduceby()); } } |
Run it then you will get the result as below.
1 2 3 |
********** Display fruit bean ********** name = Apple produceby = Indonesia |
Finally, you are familiar to the tutorial create spring application in Eclipse. In the next time, hope that you could create your own spring application.
Happy learing!