Spring Hello World Example Use Maven And Eclipse

This article will show you how to use maven to download, configure Spring framework in your java project. And how to write Hello World example use Spring.

1. Create Java Project Use Maven.

If you do not know how to do this in maven, you can read Create Java Project With Maven. Then you can run below commands in command line.

  1. cd C:\WorkSpace\dev2qa.com to the directory where you want to save the project.
  2. mvn archetype:generate -DgroupId=com.dev2qa.core -DartifactId=Spring4ExampleProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  3. Just click Enter key if you meet any number choice prompt.
  4. After the command execute complete, you can see the java project folder has been created under your current directory.
    maven craete java project soccessfully
    maven created java project folder

2. Convert Java Project To Eclipse Project.

  1. Run cd Spring4ExampleProject go into it.
  2. Run mvn eclipse:eclipse to convert.
    convert maven style java project to eclipse style
  3. After this, you can import the project into Eclipse. You can read Manage Maven Project Using Eclipse to learn how to do that.

3. Add Spring 4.3.10 Dependency In Java Project.

  1. Go to Spring Framework download page, get the latest version pom xml.
    latest spring version pom xml
  2. Copy above dependency xml into your java project pom.xml like below.
     <dependency>
    
     <groupId>org.springframework</groupId>
    
     <artifactId>spring-core</artifactId>
    
     <version>4.3.10.RELEASE</version>
    
     </dependency>
     <dependency>
    
     <groupId>org.springframework</groupId>
    
     <artifactId>spring-context</artifactId>
    
     <version>4.3.10.RELEASE</version>
    
     </dependency>

    add spring dependency pom xml

  3. Now run mvn dependency:resolve in command line to get all the dependency jars.
    resolve spring dependency jars

4. Add The Spring Dependency Jars Into Eclipse Project.

  1. After dependency resolve, you can find below jar folder in your local maven repository directory.
    local maven repository spring jar folder
  2. Right click your project name in Eclipse, click “Build Path —> Configure Build Path”.
    configure java build path in java project
  3. Click “Add External JARs” button in the popup dialog, choose each of above jars. There are totally 5 jars. They are spring-expression-4.3.10.RELEASE.jarspring-core-4.3.10.RELEASE.jarspring-context-4.3.10.RELEASE.jarspring-beans-4.3.10.RELEASE.jar, spring-aop-4.3.10.RELEASE.jar
    add spring jars into eclipse project

5. Create HelloWorldExample Java Class.

package com.dev2qa.core.spring;

public class HelloWorldExample {

	private String userName = "";

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void printHello()
	{
		System.out.println("Hello " + this.getUserName() + ", you are welcome to Spring world.");
	}
}

You can see it in Eclipse project left panel as below.

Spring HelloWorld example java bean

6. Create SpringBeansConfig.xml

  1. Create a resource folder under src/main.
  2. Create SpringBeansConfig.xml under it, and add bean definition in it.
     <bean id="helloWorldBean" class="com.dev2qa.core.spring.HelloWorldExample">
     <property name="userName" value="dev2qa" />
     </bean>

7. Invoke HelloWorldExample Bean.

Input below java code in class com.dev2qa.core.App.

    
    public static void main( String[] args )
    {
    	/* Get Spring context. */
    	ApplicationContext springContext = new ClassPathXmlApplicationContext("SpringBeansConfig.xml");

    	/* Get HelloWorldExample bean by it's id. */
		HelloWorldExample instance = (HelloWorldExample) springContext.getBean("helloWorldBean");
		
		/* Invoke the bean's method. */
		instance.printHello();
    }

When you run it, you may encounter below exception.java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
commons logging exception during run spring

8. Resolve Commons Logging Exception.

  1. Add below commons logging dependency xml in pom.xml.
      <dependency>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     <version>1.1.1</version>
     </dependency>
    
  2. Run mvn dependency:resolve in command line to download the commons-logging jar file into your local maven repository.
  3. Add commons-logging-1.1.1.jar located at %MVN_Repository_Home%\commons-logging\commons-logging\1.1.1 into your Eclipse project build path.
  4. Now run App.class again, you can see following output.

Spring Hello World Example output

9. Source Code

  1. Download “SpringHelloWorldExample.zip” SpringHelloWorldExample.zip – Downloaded 91 times – 11 KB

(Visited 549 times, 4 visits today)
READ :   Spring Dependency Injection Explanation Use Loosely Coupled Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.