Home >Java >javaTutorial >Build a simple spring boot project example

Build a simple spring boot project example

零下一度
零下一度Original
2018-05-18 14:10:203475browse

Introduction

  • Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses an ad hoc approach to configuration, eliminating the need for developers to define boilerplate configurations. In this way, Spring Boot strives to become a leader in the booming field of rapid application development.

  • In previous spring projects, we would face a lot of tedious configurations, and basically use them with a lot of copy and paste. Spring Boot allows us to easily and quickly build Spring Web applications without excessive configuration. It can be used out of the box without code generation or XML configuration, allowing us to quickly use the spring framework.

Start

Version: java 1.8.0_51 & spring boot 1.5.4

1. Build a simple spring boot project

The generator SPRING INITIALIZR provided on the official website here is used to create a simple spring boot project.

1. Visit http://start.spring.io

Spring INITIALIZR.png

Options: Project (maven) Language (java) SpringBoot version (1.5.4)
Group fills in the group name, Artifact fills in the module name, Dependencies on the right side can select the corresponding dependencies, because we want to build a web project, so we can add web dependencies.
Click Generate Project to generate the download project.

2. Import the downloaded maven project into the IDE and run it

Extract the downloaded project and import it into the IDE (IntelliJ IDEA is used here)
As follows:

spring boot.png

Directly run the main method of DemoApplication.java.
Screenshot of successful operation:

Build a simple spring boot project example

You can see that the process ID of the project is: 25642, and you can view the detailed information through java's jconsole tool.
You can see that the startup port of the project is 8080 (spring boot default port, which can be modified in application.properties)

Build a simple spring boot project example

2. pom.xml Explain

Open the pom.xml file and view the configuration information

Inherit the parent module. The spring-boot-starter-parent module contains automatic configuration, logs and YAML (reference:), so Building spring projects becomes easy.

 <!-- 继承 spring boot 父包-->
<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.4.RELEASE</version>
    <relativepath></relativepath> <!-- lookup parent from repository -->
</parent>

pom.xml relies on the spring-boot-starter-web module, including Tomcat and spring-webmvc (reference:). There is no need to specify the version because the parent module already has the default configuration. If needed Specified versions can be added.

<!-- 构建web项目模块 包括了Tomcat和spring-webmvc -->
<!-- spring-boot-starter-web 默认依赖了tomcat的starter 所以使得项目可以直接运行而不需要部署到tomcat中-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

pom.xml depends on the spring-boot-starter-test test module, including JUnit, Hamcrest, Mockito

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
In spring-boot, module dependencies They are all done in the starter format and named in the spring-boot-starter- format, with indicating the specific module. The spring-boot ecosystem provides a wealth of starters for developers to use (reference:)
This modular dependency simplifies a large number of dependency configurations. Using the starter dependency method, you can depend on the corresponding dependent packages into the project together, unlike traditional Spring which requires separate dependent packages.
For example, the web module in spring-boot needs to rely on web service packages such as org.springframework spring-web in traditional Spring configuration. In spring-boot, it only needs to rely on spring-boot-starter- The web can depend on the corresponding packages, greatly simplifying the configuration.

Supplementary

The above explains that the spring-boot project can be built directly from http://start.spring.io
The following explains directly in IntelliJ IDEA Build the spring-boot project

1. Open IDEA and create a new project

new project.png

2. Select Spring Initializr and fill in https:// in Choose Initializr Service URL. start.spring.io, click Next

spring i.png

##3. Fill in Group Artifact and other relevant information, click Next

Build a simple spring boot project example

4. Select the modules you need to depend on and click Next

Build a simple spring boot project example

The above is the detailed content of Build a simple spring boot project example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn