So far we have seen, what is spring framework and why to use it in Java application development. In this particular article we will see how to get started with spring in Eclipse and how to setup a initial Spring project.

Lets take a look at required things before we try to setup a spring project in Eclipse IDE, following are minimal requirements.

1) Java 6+
2) Maven
3) Eclipse IDE

Setup Spring project in Eclipse using Maven

1) Requirement Testing

1.1) Java : Before we start configuring lets test our system for required tools, make sure you have Java installed in your class path:

techie@techie-Satellite-L50-B:~$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)


1.2) Now lets test for maven installation, supply following command to test if maven is already there:

techie@techie-Satellite-L50-B:~$ mvn -version
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: /usr/lib/jvm/jdk1.8.0_60/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-50-generic", arch: "amd64", family: "unix"


1.3) Make sure you have a working Eclipse IDE with you.

2) Create a simple Java project with maven

Supply following command to command line, here -DgroupId=com.tbNext.sample is the base package of the application and -DartifactId=SampleSpringMaven is the name of project.

mvn archetype:generate -DgroupId=com.tbNext.sample -DartifactId=SampleSpringMaven -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


Now you should see a BUILD SUCCESS massage something like this:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: /home/techie
[INFO] Parameter: package, Value: com.tbNext.sample
[INFO] Parameter: groupId, Value: com.tbNext.sample
[INFO] Parameter: artifactId, Value: SampleSpringMaven
[INFO] Parameter: packageName, Value: com.tbNext.sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/techie/SampleSpringMaven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.666s
[INFO] Finished at: Sat Oct 03 14:52:49 IST 2015
[INFO] Final Memory: 11M/124M
[INFO] ------------------------------------------------------------------------


3) Make maven project compatible to eclipse

By default maven projects are not compatible to eclipse, just go inside the project directory and supply following command to make it compilable to Ecllipse IDE.

techie@techie-Satellite-L50-B:~$ cd SampleSpringMaven/ techie@techie-Satellite-L50-B:~/SampleSpringMaven$ mvn eclipse:eclipse

If everything goes right, you should see something like below on your console:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SampleSpringMaven Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) @ SampleSpringMaven >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) @ SampleSpringMaven <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ SampleSpringMaven ---
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "SampleSpringMaven" to /home/techie/SampleSpringMaven.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.145s
[INFO] Finished at: Sat Oct 03 14:56:24 IST 2015
[INFO] Final Memory: 9M/150M
[INFO] ------------------------------------------------------------------------

Here we are done with creating a simple java project in maven.

4) Import project to eclipse

Now lets import this project to Eclipse IDE, open your ide and import the project, using import > existing project in workspace

5) Now we have simple Java project imported in eclipse, lets make it a spring project.


5.1) Adding spring dependency(pom.xml)
Replace pom.xml content with following:


	4.0.0
	com.beingjavaguys.sample
	SampleSpringMaven
	jar
	1.0-SNAPSHOT
	SampleSpringMaven Maven Webapp
	http://maven.apache.org

	
		4.2.1.RELEASE
		1.8
	

	
		
			org.springframework
			spring-context
			${spring.version}
		
	

	
		SampleSpringMaven
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.0
				
					${jdk.version}
					${jdk.version}
				
			
		
	



Now go back to maven console and supply following command onece again, it will download and add required dependencies to the project:

mvn eclipse:eclipse


Go back to Eclipse, refresh the project and make sure you have all the required dependencies in Project > Properties > Java Buid Path > Libraries

5.2) Add a spring configuration file /src/main/resources/spring-conf.xml Add a spring configuration file to the project resources folder, with following content


 

All the application beans entries will go here, we are done with creating a Spring project uisng maven.

6) Run application

6.1) Create a sample bean class
src/main/java/com/tbNext/beans/SampleBean.java

package com.tbNext.beans;

public class SampleBean {
	public void printMe() {
		System.out.println("Hello World !");
	}
}

6.2) Add the entry to spring-conf.xml


        
	
	


6.3) Add a application class with main method: /src/main/java/com/tbNext/App/Application.java

package com.tbNext.App;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tbNext.beans.SampleBean;

public class Application {
	public static void main(String args[]) {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"spring-conf.xml");

		SampleBean bean = (SampleBean) applicationContext.getBean("sampleBean");
		bean.printMe();
	}

}

6.4) Now run project as Java Application, you should see Hello World printed on the console.

Download Complete Project Scource Code from: Here

Here we are done with creating a simple spring project using maven and importing it in eclipse. In upcoming blogs we will see more about spring framework.

  • By Techburps.com
  • Oct 7, 2015
  • Spring Framework