Spring Initializr [start.spring. IO /] is a great way to get you started on a quick Spring Boot project.
It allows you to build a complete Spring Boot application with simple steps. You can build the following types of Spring Boot applications using the Spring Initializr Boot interface:
-
Web applications
-
Restful applications
-
Batch application
Spring Boot provides good support for many third-party frameworks, which are available through the corresponding artifactId. Here are some of them for your reference:
- Spring-boot-starter-web-services: Used to build SOAP Web service applications that can interact with the outside world
- Spring-boot-starter-web: it can be used to build Web applications or Restful applications
- Spring-boot-starter-test: Can be used to build and write unit and integration tests
- Spring-boot-starter-jdbc: Builds jDBC-based applications
- Spring-boot-starter-hateoas: Introduces the Hateoas function to facilitate RESTful service implementation
- Spring-boot-starter-security: uses Spring Security to authenticate and authenticate system users
- Spring-boot-starter-data-jpa: Spring data JPA implemented based on Hibernate
- Spring-boot-starter-cache: enables cache support based on the Spring Framework
- Spring-boot-starter-data-rest: uses Spring data REST to provide REST services
In this lecture, I’ll demonstrate how to quickly create a simple Web application by using Spring Initializr.
Build Web applications using Spring Initializr
Building a Web application using Spring Initializr is very easy and quick.
As shown in the figure above, we need to do the following:
- Access Spring Initializr from a browserwebsite, and then execute the following options
- Set the groupId: com. Ramostear. Spring. The boot
- Set the artifactId: spring – the boot – quick – start
- Project name: The default value is spring-boot-quick-start
- Base package name: default (you can also choose to change) (click More Options to expand)
- In the search box, search and select the following components: Web,Actuator, and DevTools
- Finally, click ** “Generate Project” ** to Generate and download the Project
- Import the project into IntelleJ IDEA
Spring Boot project directory structure
The following figure shows importing the project directory structure you just downloaded into IDEA:
- SpringBootQuickStartApplication. Java: Spring Boot run master file, it is responsible for initializing the Spring Boot automatically configure and Spring application context
- Application.properties: Application configuration file
- SpringBootQuickStartApplicationTests: simple starter used in testing
- Pom.xml: Maven build project configuration file, including the Spring Boot Starter Web dependencies. In particular, it automatically makes the Spring Boot Starter Parent the Parent of the entire project.
Core code
The SRC /main/ Java package places our main logical code, the SRC /test/ Java package places the test code of the project, the SRC /main/ Resources package places the configuration file of the project and some static resource files, such as page HTML files, CSS files and JS files. Let’s go from top to bottom.
SpringBootQuickStartApplication.java
package com.ramostear.spring.boot.springbootquickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootQuickStartApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootQuickStartApplication.class, args); }}Copy the code
- @SpringBootApplication: Responsible for initializing SpringBoot automation configuration items and the Spring application context
- Springapplication.run () : the static method responsible for launching the Spring Boot application
application.properties
The default port is 8080 and the name of the application is Spring Boot Quick Start:
server.port= 8080
spring.application.name= Spring Boot Quick Start
Copy the code
SpringBootQuickStartApplicationTests
package com.ramostear.spring.boot.springbootquickstart;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootQuickStartApplicationTests {
@Test
public void contextLoads() {}}Copy the code
- Integrates all the environments needed to unit test Spring Boot applications
Run the application
As shown above, click the Run button to run the Spring Boot application. After successful startup, you will see the following log message output from the console:
"C:\Program Files\Java\jdk1.8.0 comes with _144\bin\java"... .____ ____ _ /\\/ ___'_ ___ _(_)_ __ ___\ \ \ \
(()\__ _ __ | '|' | | '_\/_ ` |\ \ \ \
\\/ ___) | | _) | | | | | | | (_ | |)))) 'there comes | |. __ _ - | | | _ _ - | | | _\__, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = | ___ / = / _ / _ / _ / : : Spring the Boot: : (v2.1.4. RELEASE)... The 16:44:16 2019-04-21. 2320-784 the INFO/restartedMain O.S.B.A.E.W eb. EndpointLinksResolver: 5 - [Exposing 2 endpoint(S) : Drive to the Exposing 5 - o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (HTTP) with context path "2019-04-21 16:44:16.852 INFO 2320 -- [restartedMain] .r.s.b.s.SpringBootQuickStartApplication : Started SpringBootQuickStartApplication in 3.526 seconds (JVM running for 5.448)Copy the code
conclusion
Now that you’ve learned how to quickly build a Spring Boot Weby9 application using Spring Initializr, you can download the full source code for this article on Github.
Author: Tan Chaohong
Original :(lecture 1)Spring Initializr- the best choice for a quick start with Spring Boot