Introduction to SpringBoot (IDEA) (2)

1. 3 ways to start SpringBoot

  The first one: start run as directly with the help of IDE tools

  The second: mvn command to start

    1: Open the command line and enter the project directory (I still use the dog project created last time to operate) cd E:\Workspaces\Idea\dog  

    2:mvn spring-boot:run

  The third type: start with a jar file

    1: Compile the project in the project directory (ie E:\Workspaces\Idea\dog)

      mvn install

    2: Enter the target directory

      cd target/

    3: View all files and directories under the current path

      to you ./

    4: Find the jar file corresponding to the project, here is dog-0.0.1-SNAPSHOT.jar

      Then run the java command:

      java -jar java -jar dog-0.0.1-SNAPSHOT.jar

Second, the configuration file of SpringBoot

  1, properties or yml

  By default, the resources directory is generated for us, and our configuration files are basically perfected in this directory.

  There is also an application.properties file provided by default in the directory, where we can configure the access port and path

server.port=8081
server.servlet.context-path=/dog

    Here, the default port is modified to 8081, and the access path is modified. So we can no longer use the original http://127.0.0.1:8080/hello

  Come say hello, you need to use the new access address http://127.0.0.1:8081/dog/hello

  Another convenient way of writing is also provided in SpringBoot, which is the yml file

server:
port: 8082
servlet:
context-path: /dog

  Note that there needs to be a space after the colon to separate it from the content of the configuration

  2. Multi-environment configuration

    Add 2 yml files to configure different access ports and different access paths respectively

    Configuration of the new development environment: application-dev.yml file 

server:
port: 8083
servlet:
context-path: /dev

    Configuration of the new production environment: application-pro.yml file

server:
port: 8084
servlet:
context-path: /pro

   Modify the application.yml file to specify the configuration of the environment to be used, here we point to the development environment

spring:
  profiles:
    active: dev

  Note: The prefix here is changed to spring, which is no longer server. Access is http://127.0.0.1:8083/dev/hello

  3. Start multiple environments at the same time

    The third startup method described above is used here: java -jar method to start

    When using the startup command, we only need to add some parameters to easily specify the configuration environment we want to use

    The first step: compile mvn before starting (because we have changed the project, we can no longer use the original jar file to start)

    第二步:java -jar dog-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

    Step 3: Open a new command line window, enter the target directory of the project, and execute the java -jar command

        java -jar dog-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro

    Step 4: Test http://127.0.0.1:8083/dev/hello http://127.0.0.1:8084/pro/hello can be accessed and successful

  4. Other configuration

    a. Some unrelated values ​​in the configuration are read in the project

      Here we operate in the dev environment and add some values ​​to be read in the application-dev.yml file

        

 

 

      Then pull in the foreground, here use the @Value annotation to pull

    Front-end display successfully

      

 

   b. Use configuration in configuration

    Use EL expressions in yml files

    

    The controller still uses @Value to pull

    

  c. Related properties, which can be sealed and converted into objects

    Step 1: Add dependencies to pom.xml

        <!--configuration的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

    Step 2: Create a new method to encapsulate properties, provide getter and setter methods, and override the toString() method

     

package com.zmfx.hello;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component   // This class can be scanned when @Autowired is automatically assembled later 
@ConfigurationProperties(prefix = "dev") // Indicates that the prefix in the configuration file is dev packaged into an object 
public  class Developer {
     private String name; // Name 
    private Integer age; // age 
    private Integer id; // number

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Developer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}

    Step 3: Add attributes to the yml file, I still add it in the development environment application-dev.yml

server:
  port: 8083
  servlet:
    context-path: /dev
pageSize: 20
devName: Zimo Feixue

content: "pageSize:${pageSize},devName:${devName}"

dev:
  name: Zimo Feixue
  age: 26
  id: 365

    Step 4: Write the control code, new properties and methods in the controller

  

// The property is encapsulated into the object to extract 
    @Autowired
     private Developer developer;
    @RequestMapping(value = "/developer",method = RequestMethod.GET)
    public String showDeveloper(){
        return developer.toString();
    }

    Step 5: Enter http://127.0.0.1:8083/dev/developer in the browser to test

 

Third, the use of SpringBoot template (template)

  First let's introduce @RestController

    It is equivalent to a shorthand for @Controller+@ResponseBody.

    When @ResponseBody is used, the return value is echoed to the page, and it is parsed into Json format by default. won't walk the view resolver

    If you replace @RestController with @Controller, the page will report 500, prompting you to check the view resolver.

  SpringBoot officially provides us with a template thymeleaf for page display

  Step 1: Add pom.xml dependencies

     <!--模板的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

  Step 2: Write an index.html page under templates

  

<h1>Hello SpringBoot!</h1>

  Step 3: Write the code of the controller layer

//templates的展示
    @RequestMapping(value = {"/t","/temp","/template"} ,method = RequestMethod.GET)
    public String showIndexHtml(){
        return "index";
    }

   Step 4: Visit http://127.0.0.1:8083/dev/t 

            http://127.0.0.1:8083/dev/temp

          http://127.0.0.1:8083/dev/template

We found that its mapping rules are not much different from those in SpringMVC, but you don't have to configure the view resolver yourself

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724783&siteId=291194637