This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Spring Boot

SpringBoot, the current mainstream development framework, for the cumbersome SSM development, has made a great simplification

Basic operation

New Springboot

For the New SpringBoot project, strongly recommended IDEA, where the Spring Initializr option, one click new

When selecting the server URL, it is not recommended to change to the Ali mirror address, but to use the native start.spring. IO, which is to consider the structure specification of the SpringBoot project

For other dependencies such as type, language, packaging, etc., make your own selection and then select the dependencies you want to add from the menu bar

Receive parameters

The simple but important part of receiving parameters is described here

  1. @pathvariable: Dynamic receive parameters, with the parameters in {} consistent with the formal parameters

    • http://localhost:8080/index/122
    @RequestMapping("/index/{id}")
    @ResponseBody
    public String index(@PathVariable(value = "id") String id) {
        return "id:" + id;
    }
    Copy the code
  2. RequestParam: Receive parameters dynamically, bound to the passed parameters by the annotated parameters, no parameters are required

    • http://localhost:8080/index?id=122
    @RequestMapping("/index")
    @ResponseBody
    public String index(@RequestParam("id") String name) {
        return "id:" + name;
    }
    Copy the code
  3. Pass parameters without annotations: Map the parameters passed directly

    • http://localhost:8080/index?id=122&name= Zhou Mo
    @RequestMapping("/index")
    @ResponseBody
    public String index(String id, String name) {
        return id + ":" + name;
    }
    Copy the code
  4. HttpServletRequest Pass Parameters: Arguments are passed through the HttpServletRequest scope

    • http://localhost:8080/index?id=122
    @RequestMapping("/index")
    @ResponseBody
    public String index(HttpServletRequest request) {
        String id = request.getParameter("id");
        return "id:" + id;
    }
    Copy the code

Both of the above annotations can accept dynamic parameters, but the difference is. @pathVariable uses/to delimit parameters, and @requestParam uses? ; In addition, both annotations can be bound to the request parameters using the built-in parameters of the annotations, which can be arbitrary

The configuration file

SpringBoot provides two configuration files in the properties and YML formats. The default is properties, described here in YML format

In addition, for configuration files in both formats, the YML file takes precedence over the Properties file. How to read the SpringBoot configuration file

Person:
  name: Zhou Mo
Copy the code
@Controller
@PropertySource(value = {"classpath:application.yml"}, encoding = "UTF-8")
public class indexController {

    @Value("${Person.name}")
    private String name;

}
Copy the code

When reading a configuration file in Chinese, garbled characters may exist. My solutions to this are as follows (they vary, for reference only)

  • File > Settings > Editor > File Encoding, change the default encoding of the properties file to UTF-8, and checkAutomatically converts to Ascii but displays native content
  • Delete the original configuration file and copy it again. You can also delete the configuration items and paste them back. The core is to rewrite the configuration items related to Chinese

At present, my problem has been successfully solved by the above two steps

Multi-environment configuration

Profile, environment!

For projects built by SpringBoot, you can switch configuration files depending on the environment

applcation.properties
application-dev.properties
application-test.properties
application-prod.properties
Copy the code

In Maven’s resource directory, create the four profiles described above, representing the main profile, development environment, Test environment, and production environment. For properties, YML, depending on personal preferences and usage scenarios, the following uses YML to describe the configuration of multiple environments

Applcation. Yml reads the configuration file first and specifies the actual application, as shown below

# master configuration

spring:
  profiles:
    active: dev

server:
  port: 10100

---

# Development environment

server:
  port: 9090
spring:
  config:
    activate:
      on-profile: dev
---

# Test environment

server:
  port: 7070
spring:
  config:
    activate:
      on-profile: test
Copy the code

Configuration files for multiple scenarios are written together to be split. Of course, if you have too many configuration items, you can split the file. In the above configuration file, the master configuration file, specifies dev as the current configuration file, so the service port is 9090

Hot deployment

You’re familiar with hot deployment, and here’s how it’s implemented in SpringBoot

Import dependencies in the pom. XML file, or select Add when you create a SpringBoot project

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
Copy the code

Also, don’t forget the plug-in configuration in POM.xml. The default Maven project does not do this

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

If you use the IDEA tool, you need to perform the following Settings

  • File -> Settings -> Build, Execute, deploy -> Compiler, checkAutomatically build projects

There are several ways to implement SpringBoot hot deployment, even through third-party IDEA plug-ins

The last

Some common SpringBoot operations, next time definitely