Spring boot sorting (build environment + basic use)

Using idea to create a spring Boot project

Add web dependency support

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
>Run first SpringBoot project
@RestController
public class FirstDay {
    @RequestMapping("/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
server:
  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
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
    @Value("chinese rhubarb")
    private String name;
    @Value("3")
    private  int age;
}
    @Autowired
     private Dog dog;
    @Test
    void contextLoads() {
        System.out.println(dog);
    }

Method 2: use configuration file

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@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

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

  • Dependencies required to use annotation @ ConfigurationProperties(prefix = "person")
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

Change to correct format

Custom banner


Find banner on Baidu and cv go to banner Txt file

Multi environment configuration and file location

priority

Activate multi file configuration properties form

server.port=8083
server.port=8083
#Multi file configuration of spring boot: you can choose which file to activate
spring.profiles.active=dev

yaml syntax configuration

server:
  port: 8081
spring:
  profiles: 
  active: dev
---
server:
  port: 8081
spring: 
  profiles: dev
---
server:
  port: 8082
spring:
  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
  <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

The first Thymeleaf program

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

Introduction to Thymeleaf grammar

  • Accessing static resources

    th:src="@{bootstrap/js/boostrap.min.js}"

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

    th:text="${user.name}"

  • Iterate over a userlist collection

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

MVC configuration principle

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

Keywords: Java Spring Boot Back-end intellij-idea

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