Two, SpringBoot entry - Creating project

1. Create SpringBoot project with IDEA in Spring Initializr

When I came into contact with and learn the Spring Framework by the end of 2015, often distressed those tedious XML configuration file, this time I'll try to use Spring Boot, can more easily get started more quickly and easily build Spring applications!

Spring Boot Let Spring application becomes more weight. We do not have to build as before cumbersome items, packaged applications, such as deploying to Tomcat application server to run our business services. Spring Boot implemented by the service, only need to rely on a Java class, package it into a jar, and through the java -jar command can be up and running. All this compared to traditional Spring application, has become very lightweight and simple.

SpringBoot project to create direct use IDEA plug-Spring Initializr, you can create the official website, and then download it into the IDEA.

Create a project

Select Spring Initializr

Configuration information

Then Next to select the frame where, first of all let's create a simple project that requires selecting one of these tools framework

  1. Web
    1. Spring Web

Once it's created, directly after opening Demo01Application.java run the main method, and then open the browser to access localhost: 8080

Startup class

So you do not configure the path mapped access to Error page

The emergence of this page says the project has been properly started.

2. Structural Analysis

Introduction 2.1 file structure

  • src/main/java
    • Java code, including tools, Controller, Service, etc.
    • Demo01Application.java class is the entry program
  • src/main/resources
    • Configuration files, static files, etc.
    • application.properties is the default configuration file
  • src/test/
    • Java code for testing

2.2 Introduction Project Dependencies

<?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.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.magic</groupId>
    <artifactId>demo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo01</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

复制代码

It can be divided into four parts

  • Project information: including groupId, artifactId, version, name, description tag
  • Inheritance relationship: parent label stating that the project inherited from the spring-boot-starter-parent, and declared to be inherited from 2.2.0.RELEASE version
  • jar package depends: dependencies tag contains a specific case depend jar package, such as spring-boot-starter-web, spring-boot-starter-test.
  • Construction configuration: build tag may be constructed Maven control parameters, default spring-boot-maven-plugin, with the spring-boot-starter-parent application can be packaged into a JAR file SpringBoot directly run

3. Create and test a Controller

Create a controller / IndexController.java in src / main / java / under

code show as below

@RestController
public class IndexController {

    @RequestMapping("/index")
    public String index(){
        return "Hello World!";
    }

}
复制代码

After running the program again, visit http: // localhost: 8080 / index may see the following interface

4. Profiles

First, SpringBoot project Spring Initializr created a default configuration file is application.properties, the wording of this profile and write me feel TXT text makes no difference, I prefer the profile yml format that use application.yml YAML syntax , here to give you a brief introduction about the properties YAML configuration file.

What is the 4.1 YAML

4.2 Basics

First of all to remove the default file application.properties, because the priority of the profile is higher than the yml. Then you can create application.yml file to use, you do not need additional configuration.

Secondly, YAML syntax is set according to indent the code segment, colon and a space, plus IDEA comes with code hinting and layout features, very simple to use.

server:
  # 端口号
  port: 80
  servlet:
    # 设置上下文访问路径
    context-path: /demo01
复制代码

Once set up, we run the project again, and then visit http: // localhost / demo01 / index may see the same results

Guess you like

Origin juejin.im/post/5dc13038e51d456f28370fe3
Recommended