Spring boot sorting (build environment + basic use)

Using idea to create a spring Boot project

Add web dependency support

>Run first SpringBoot project
public class FirstDay {
    public String first(){
        return "helloSpring";

Maven operation of springBoot

Running a project using shell commands

Change port number

  • Method 1: configure properties
server.port =8081
  • Method 2: yml syntax
  port: 8085

SpringBoot launcher

  • For example, the web launcher is equivalent to the configuration file + core dependency in Spring, but you don't need to write your own handwriting for the integration of SpringBoot

yml syntax

Several ways of assigning values to attributes

  • Method 1: annotation method
public class Dog {
    @Value("chinese rhubarb")
    private String name;
    private  int age;
     private Dog dog;
    void contextLoads() {

Method 2: use configuration file

@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birthday;
    private Map<String,Object> maps;
    private List<Object> list;
    private Dog dog;

In yml file

  name: jiasen
  age: 20
  happy: true
  birthday: 2001/10/07
  maps: {q: wrong q,w: wrong w}
    - code
    - music
    name: chinese rhubarb
    age: 3

  • Dependencies required to use annotation @ ConfigurationProperties(prefix = "person")

Custom file

JSR303 verification

  • In springboot, @ Validated can verify the data of the model received in the background, and throw an exception if it does not meet the requirements
  • Support @ Email dependency

Change to correct format

Custom banner

Find banner on Baidu and cv go to banner Txt file

Multi environment configuration and file location


Activate multi file configuration properties form

#Multi file configuration of spring boot: you can choose which file to activate

yaml syntax configuration

  port: 8081
  active: dev
  port: 8081
  profiles: dev
  port: 8082
  profiles: test

Principle of automatic assembly

  • SpringBoot loads a large number of auto configuration classes
  • Are our requirements in the automatic configuration written by SpringBoot by default
  • Let's look at which components are configured in the auto configuration class
  • When adding components to the automatic configuration class in the container, some properties will be obtained from the properties class. We just need to specify the values of these properties in the configuration file
  • Xxxmaautoconfiguration: automatic configuration class; Add components to container
  • XXXXProperties: encapsulates the related properties in the configuration file

Spring web development

  • Research on static resource introduction

Resource exploration

  • Priority: public > resources

Homepage and icon customization

Thymeleaf template engine

  • Add dependency

The first Thymeleaf program

    public String second(Model model){
        model.addAttribute("msg","hello thymeleaf");
        return "test";

Introduction to Thymeleaf grammar

  • Accessing static resources


  • Access the data in the model, such as the name attribute of a user object


  • Iterate over a userlist collection

<tr th:each="user : ${userlist}">
    <td th:text="${user.name}">tyrone</td>
    <td th:text="${user.age}">18</td>
  • Judge whether it is empty
<tr th:if="${messages.empty}">
    <td colspan="3">No messages</td>
  • Use in js
<script th:inline="javascript">
    var user = [[${user}]]
    console.log(user.name + "\t" + user.age);

MVC configuration principle

//If you want to customize your own functions, just rewrite this component and give it to springBoot
public class configOne implements WebMvcConfigurer {
    //Customize a view parser MyViewResolver
    public static class MyViewResolver implements ViewResolver{
            public View resolveViewName(String viewName, Locale locale) throws Exception {
                return null;
        //ViewResolver implements its own view parser myViewResolver
        public ViewResolver myViewResolver(){
            return new MyViewResolver();
  • Override view jump
public class MyMvcConfig implements WebMvcConfigurer {
    public void addViewControllers(ViewControllerRegistry registry) {

Keywords: Java Spring Boot Back-end intellij-idea

Added by cachemony on Thu, 03 Feb 2022 16:21:04 +0200