Thursday 13 October 2016

What is Spring Boot Initializr ?

The Spring Initializr is ultimately a web application (at “http://start.spring.io/”) that can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code with.

Spring Initializr can be used in several ways:
  • Through a web-based interface
  • Via Spring Tool Suite
  • Via IntelliJ IDEA
  • Using the Spring Boot CLI
Why we need Spring Boot Initializr?
Sometimes the hardest part of a project is getting started. You need to set up a directory structure for various project artifacts, create a build file, and populate the build file with dependencies. The Spring Boot CLI removes much of this setup work, but if you favor a more traditional Java project structure, you’ll want to look at the Spring Initializr.

USING SPRING INITIALIZR’S WEB INTERFACE
Now we are starting with the web-based interface.

The Spring Team has provided a Web Interface for Spring Boot Initializr at “http://start.spring.io/”. We can use it to create our new Project’s base structure for Maven/Gradle build tools.

Creating Maven Example with Spring Boot Initializr Web Interface
There are following steps to create new Spring Boot Web Application for Maven Build tool and Spring STS Suite IDE.

Step 1: Go to Spring Boot Initializr at “http://start.spring.io/”.

Step 2: Once you’ve filled in the form and made your dependency selections, click the Generate Project button to have Spring Initializr generate a project for you.
Step 3: Now click on “Generate Project” Button, it creates and downloads a Maven Project as “myapp.zip” file into our local file system.

Step 4: Move “myapp.zip” to our Spring STS Suite Workspace and Extract this zip file
Step 5: Import this “myapp” Maven project into Spring STS IDE.

Step 6: We’d have a project structure similar.

If you observe this project files, it generates pom.xml file, two Spring Boot Java files and one JUnit Java file.
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  <modelVersion>4.0.0</modelVersion>
  5.  <groupId>com.dineshonjava</groupId>
  6.  <artifactId>myapp</artifactId>
  7.  <version>0.0.1-SNAPSHOT</version>
  8.  <packaging>jar</packaging>
  9.  <name>myapp</name>
  10.  <description>Demo project for Spring Boot</description>
  11.  <parent>
  12.   <groupId>org.springframework.boot</groupId>
  13.   <artifactId>spring-boot-starter-parent</artifactId>
  14.   <version>1.3.5.RELEASE</version>
  15.   <relativePath/> <!-- lookup parent from repository -->
  16.  </parent>
  17.  <properties>
  18.   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.   <java.version>1.8</java.version>
  20.  </properties>
  21.  <dependencies>
  22.   <dependency>
  23.    <groupId>org.springframework.boot</groupId>
  24.    <artifactId>spring-boot-starter-data-jpa</artifactId>
  25.   </dependency>
  26.   <dependency>
  27.    <groupId>org.springframework.boot</groupId>
  28.    <artifactId>spring-boot-starter-web</artifactId>
  29.   </dependency>
  30.  
  31.   <dependency>
  32.    <groupId>org.springframework.boot</groupId>
  33.    <artifactId>spring-boot-starter-test</artifactId>
  34.    <scope>test</scope>
  35.   </dependency>
  36.  </dependencies>
  37.  
  38.  <build>
  39.   <plugins>
  40.    <plugin>
  41.     <groupId>org.springframework.boot</groupId>
  42.     <artifactId>spring-boot-maven-plugin</artifactId>
  43.    </plugin>
  44.   </plugins>
  45.  </build>
  46.  
  47. </project>

MyappApplication.java

  1. package com.dineshonjava;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyappApplication {
  6.  public static void main(String[] args) {
  7.   SpringApplication.run(MyappApplication.class, args);
  8.  }
  9. }


MyappApplicationTests.java
  1. package com.dineshonjava;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.boot.test.SpringApplicationConfiguration;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. import org.springframework.test.context.web.WebAppConfiguration;
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @SpringApplicationConfiguration(classes = MyappApplication.class)
  9. @WebAppConfiguration
  10. public class MyappApplicationTests {
  11.  @Test
  12.  public void contextLoads() {
  13.  }
  14. }

Summary

Whether you use Initializr’s web-based interface, create your projects from Spring Tool Suite, or use the Spring Boot CLI to initialize a project, projects created using the Spring Boot Initializr have a familiar project layout, not unlike other Java projects you may have developed before.

Spring Boot is an exciting new way to develop Spring applications with minimal friction from the framework itself. Auto-configuration eliminates much of the boilerplate configuration that infests traditional Spring applications. Spring Boot starters enable you to specify build dependencies by what they offer rather than use explicit library names and version. The Spring Boot CLI takes Spring Boot’s frictionless development model to a whole new level by enabling quick and easy development with Groovy from the command line. And the Actuator lets you look inside your running application to see what and how Spring Boot has done.


No comments:

Post a Comment