Spring Hello World Example Use Maven And Eclipse

This article will show you how to use maven to download, configure the 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 the below commands in the command line.

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

2. Convert Java Project To Eclipse Project.

  1. Run the command cd Spring4ExampleProject to go into the directory.
  2. Run the command mvn eclipse:eclipse to convert the Spring project into an eclipse project.
  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 the Spring Framework download page, get the latest spring project dependencies pom XML data.
  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>
  3. Now run mvn dependency:resolve in the command line to get all the dependency jars.

4. Add The Spring Dependency Jars Into Eclipse Project.

  1. After resolve the spring project dependency, you can find the spring framework-related jar folder in your local maven repository directory.
  2. Right-click your project name in Eclipse, click “Build Path —> Configure Build Path“.
  3. Click the “Add External JARs” button in the popup dialog, choose each of the above jars. There are totally of 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

5. Create HelloWorldExample Java Class.

  1. Create a java source file HelloWorldExample.java in the spring project, below is the content of the HelloWorldExample.java file.
    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.");
    	}
    }
    

6. Create SpringBeansConfig.xml。

  1. Create a resource folder under src/main.
  2. Create the XML file src/main/resource/SpringBeansConfig.xml, and add bean definition to it.
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="helloWorldBean" class="com.dev2qa.core.spring.HelloWorldExample">
            <property name="userName" value="dev2qa" />
        </bean>
    
    </beans>

7. Invoke HelloWorldExample Bean.

  1. 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();
    }
    
  2. When you run it, you may encounter below exception.java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

8. Resolve Commons Logging Exception.

  1. Add below commons-logging dependency XML in the spring project pom.xml.
     <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
     </dependency>
    
  2. Run mvn dependency:resolve in the 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 the following output.
    Hello dev2qa, you are welcome to Spring world.

9. The Example Spring Project Source Files Structure.

  1. Below is this example Spring project source files structure.
    C:\WORKSPACE\WORK\DEV2QA.COM-EXAMPLE-CODE\SPRING4EXAMPLEPROJECT
    │   .classpath
    │   .project
    │   pom.xml
    │
    ├───src
    │   ├───main
    │   │   ├───java
    │   │   │   └───com
    │   │   │       └───dev2qa
    │   │   │           └───core
    │   │   │               │   App.java
    │   │   │               │
    │   │   │               └───spring
    │   │   │                       HelloWorldExample.java
    │   │   │
    │   │   └───resource
    │   │           SpringBeansConfig.xml
  2. com.dev2qa.core.App.java.
    package com.dev2qa.core;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.dev2qa.core.spring.HelloWorldExample;
    
    /**
     * Hello world!
     *
     */
    public class 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();
        }
    }
    
  3. com.dev2qa.core.spring.HelloWorldExample.java.
    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.");
        }
    }
    
  4. src/main/resource/SpringBeansConfig.xml.

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="helloWorldBean" class="com.dev2qa.core.spring.HelloWorldExample">
            <property name="userName" value="dev2qa" />
        </bean>
    
    </beans>
  5. pom.xml.
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.dev2qa.core</groupId>
      <artifactId>Spring4ExampleProject</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>Spring4ExampleProject</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <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>
        
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        
      </dependencies>
    </project>
    
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x