Based on the learning from An example of building Java project using Maven we 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:

/wp-content/uploads/2016/05/clipboard1_946484.png

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

/wp-content/uploads/2016/05/clipboard2_946485.png

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

/wp-content/uploads/2016/05/clipboard3_946486.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.

/wp-content/uploads/2016/05/clipboard4_946487.png

Now we can start programming in Spring.

My project has the following hierarchy:

/wp-content/uploads/2016/05/clipboard5_946488.png

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

/wp-content/uploads/2016/05/clipboard6_946489.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”.

/wp-content/uploads/2016/05/clipboard7_946490.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:

/wp-content/uploads/2016/05/clipboard8_946491.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>
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply