Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

Build a Spring Hello world application using Maven

$
0
0

Based on the learning from An example of building Java project using Mavenwe can now use Maven to do more practical task.


I plan to create a Hello World application based on Spring framework. Instead of manually downloading Spring framework jar files and configured the usage of those jars into my Java project, I can now leverage Maven to make the whole process run automatically.

 

Install m2e - Maven integration plugin for Eclipse:

clipboard1.png

And then create a new Java project, and you can easily convert this project to a Maven project via context menu:

clipboard2.png

Once converted, you can then declare the dependency by clicking pom.xml and choose "Maven->Add Dependency" from context menu:

clipboard3.png

Enter group id, artifact id and version accordingly. Once done, the XML content should look as below:

 

<?xml version="1.0" encoding="UTF-8"?>  <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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>com.sap.MavenSandbox</groupId>     <artifactId>MavenSandbox</artifactId>     <version>0.0.1-SNAPSHOT</version>     <dependencies>            <dependency>                 <groupId>junit</groupId>                 <artifactId>junit</artifactId>                 <version>4.10</version>                 <scope>test</scope>            </dependency>            <dependency>           <groupId>org.springframework</groupId>           <artifactId>spring-context</artifactId>           <version>4.2.6.RELEASE</version>           </dependency>   </dependencies>  </project>

 

Trigger a build via mvn clean install, and Maven will automatically download the necessary jars of Spring framework and store them to .m2 folder.

clipboard4.png

Now we can start programming in Spring.

My project has the following hierarchy:

clipboard5.png

All missing import could be correctly parsed and easily fixed now.

clipboard6.png

If you would like to do some debugging on Spring framework source code, you can also download the related source code very easily via "Maven->Download Sources".

clipboard7.png

After that you could just set a breakpoint on the constructor of HelloWorld class and then study how Spring instantiates the instance of this class configured in beans.xml via reflection:

clipboard8.png

The source code of files used in the project

 

HelloWorld.java

 

package main.java.com.sap;
public class HelloWorld {    private String message;    public void setMessage(String message){       this.message  = message;    }    public HelloWorld(){     System.out.println("in constructor");    }      public void getMessage(){       System.out.println("Your Message : " + message);    }
}

 

MavenSandbox.java

 

package main.java.com.sap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MavenSandbox {
public static void main(String[] args) {  ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  HelloWorld obj = (HelloWorld) context.getBean("helloWorld");     obj.getMessage();
}
public String hello(){  return "Hello world";
}
}

 

MavenSandboxTest.java

 

package test.java.com.sap;
import static org.junit.Assert.assertEquals;
import main.java.com.sap.MavenSandbox;
import org.junit.Test;
public class MavenSandboxTest {
@Test 
public void test() {  MavenSandbox tool = new MavenSandbox();  assertEquals(tool.hello(),"Hello world");
}
}

 

beans.xml

<?xml version="1.0" encoding="UTF-8"?><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="helloWorld" class="main.java.com.sap.HelloWorld">       <property name="message" value="Hello World!"/>   </bean></beans>

Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>