Bootstrap Spring Boot Application Project

What is Spring boot? Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. Detailed Explanation here

Let’s create our first spring boot application.

Directions:

  1. Open terminal and fire:
  2. mvn archetype:generate DgroupId=com.javatab.app DartifactId=spring-boot-starter DarchetypeArtifactId=mavenarchetypequickstart DinteractiveMode=false
  3. Once maven generates initial project structure. Open your favorite IDE. Mine is Intellij. And import the project as maven project.
  4. Let’s update pom.xml. Notice that we have a parent pom for our dependencies. It helps in simplifying our dependency management, as it automatically inherit dependencies in the parent. Next thing to notice that we spring starter project dependency without any version. Actually spring provides dozens of starter dependencies which is evolving every day. These dependency are bundled and tested for automatic configuration setup which means we have write less/No configuration files. Say no to XML please !!! No let’s do the last update to the POM. Add spring boot maven plugin to POM. This starts apache embedded tomcat container. It also creates a executable FAT jar. Our deadly POM for spring boot is created.
    <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.javatab.app</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
      </parent>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <properties>
        <java.version>1.8</java.version>
      </properties>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>
  5. Now time to update main class i.e App.java. First refactor and update App.java to Application.java. Next annotate Application class with @SpringBootApplication which is equivalent to @Configuration, @EnableAutoConfiguration and @ComponentScan. Finally update main method by throwing Exception from the main method. Then start springrunner class which takes main class and args which is passed from command line.
    package com.javatab.app;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Starter!
     *
     */
    @SpringBootApplication
    public class Application
    {
        public static void main( String[] args )
        {
            SpringApplication.run(Application.class, args);
        }
    }
  6. Now let’s run our project, using springboot maven plugin. Go to project directory and run  mvn spring-boot:run This command will compile your project and run embedded tomcat server. Check the terminal, it will say our application waiting for Http request on port:8080 Let’s create a small service which we will consume.
    @RestController
    class Hello {
        @RequestMapping("/")
        public String sayHello() {
            return "You are good";
        }
    }

    Now instead of running mvn plugin to start the application, we will use the best practice. Run mvn clean package and wait to complete. Once it completes, check target folder for the executable jar. Now fire java -jar target/spring-boot-starter-1.0-SNAPSHOT.jar to run the application.nasruddin@nasruddin-studio-1555: ~-Project-spring-boot-tutorials_006

  7. Open console and hit http://localhost:8080/ to consume our service.localhost:8080 - Google Chrome_005

Source Code on GitHub

Advertisements

One thought on “Bootstrap Spring Boot Application Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s