1. SpringBoot configuration

1.1 Configuration File

SpringBoot uses a global configuration file with a fixed file name:

  • application.properties
  • application.yml

The configuration file is used to: modify the default springBoot automatic configuration.

Markup language:

Previous configuration files have mostly used XML configuration files YAML: data-centric and better suited to configuration files than JSON and XMLCopy the code
// yml
server:
    port: 8081
Copy the code

1.2 YAML syntax

  1. The basic grammar
  • Key: value Indicates a pair of key-value pairs
  • The hierarchy is controlled by indentation of Spaces
  • Properties and values are also case-sensitive
  1. The value of the writing
  • Literals: ordinary values (numbers, strings, booleans)
    • K: v.
    • Strings are not quoted by default;
    • “” Does not escape special characters in the string. Special characters are taken as their intended meaning
    • “Escapes special characters, which end up just plain string data
  • Objects (properties and values)(key-value pairs)
    • The object is k:v again
    • Note that the indentation
friends:
    lastName: zhangsan
    age: 20
Copy the code
  • Array (List, Set)

Represents an element in an array in -worth mode

pets:
    - cat
    - dog
    - pig
    
// Written in line:
pets: [cat.dog.pig]
Copy the code

1.3 Configuration File Value Injection

// yml
person:
    latName: hello
    age: 18
    boss: false
    maps:{k1: v1,k2: v2}
    lists:
        - dog
        - cat 

// Person.java

/** * ConfigationProperties: Tells SpringBoot to bind all properties in this class to the configuration file. * prefix="person": Which of the following attributes in the configuration file is mapped one by one * * The container provides this component only if it is a component in the container@ConfigurationPropertiesFeatures * /
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Map<String,Object> maps;
    private List<Object> lists;
    
    Getter/setter slightly
}

Copy the code

1.4 Difference between @Configuration and @Value

// Person.java

@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
    @Value("${person.lastName}")
    private String lastName;
    @Value("#{11*2}")
    private Integer age;

    @Value("true")
    private Boolean boss;
    private Map<String,Object> maps;
    private List<Object> lists;

    Getter/setter slightly

    public String getLastName(a) {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge(a) {
        return age;
    }

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

    public Boolean getBoss(a) {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Map<String, Object> getMaps(a) {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists(a) {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    @Override
    public String toString(a) {
        return "Person{" +
                "lastName='" + lastName + ' '' + ", age=" + age + ", boss=" + boss + ", maps=" + maps + ", lists=" + lists + '}'; } } // test.java @SpringBootTest class Springboot01HelloworldQuickApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } } // >>> Person{lastName='hello', age=22, boss=true, maps=null, lists=null}

Copy the code
@ConfigurationProperties @Value
function Batch injection of configuration file properties Designate one by one
Loose grammar support Does not support
SpEL Does not support support
JSR303 data verification support Does not support
The complex type support Does not support

If we just need to get a Value from a configuration file in some business logic, use @value.

If we write a javaBean specifically to map to a configuration file, we’ll just use @ConfigurationProperties

1.5 Other Configuration Comments

@ PropertySource (value = {” classpath: person. The properties “}) the path of the specified resource

@importSource (locations = {“classpath:beans.xml”}) Import the Spring configuration file and make the configuration file take effect.

// In actual development, we try not to use configuration files, and try to use configuration classes

// MyConfig.java
@Configuration
public class MyConfig {
    // @bean adds the return value of the method to the container; The default ID for this component in the container is the method name
    @Bean
    public String helloService(a){
        return "HelloService"; }}// test.java
@SpringBootTest
class Springboot01HelloworldQuickApplicationTests {

    @Autowired
    Person person;

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(a){
        Boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

    @Test
    public void contextLoads(a) { System.out.println(person); }}Copy the code

1.6 Profile Multi-environment support

Multiple files

Name the main profile: application-{profile}.properties/yml

Yml supports a multi-document block approach

Activate a specified profile

  1. Specify spring.profiles.active = dev in the configuration file
  2. Command-line mode--spring.profile.active = dev; You can add it in Program Arguments in Configuration of idea.

1.7 Loading Position of the Configuration File

  • file: ./config/
  • file: ./
  • classpath: /config/
  • classpath: /

The above files are loaded in descending order of priority. The configuration with a higher priority overwrites the configuration with a lower priority

2. SpringBoot and logs

2.1 Log Framework

Log facade (log abstraction layer) The logging implementation
JCL, SLF4j, jboss-Logging Log4j, JUL, Log4j2, Logback

Choose a facade on the left and an implementation on the right

Logging facade: Jboss-logging is generally not used, JCL has not been updated recently, so SLF4j is used only

Logging implementation: Logback is preferred

SLF4j and Logback are selected for SpringBoot

2.2 use SLF4j

2.2.1 How to Use it in the System

In the future development, logging methods should not call the logging implementation class, but should call methods in the logging abstraction layer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World"); }}Copy the code

3. Web development

Using springboot:

  1. To create the SpringBoot application, select the module we want
  2. SpringBoot has these scenarios configured by default and only needs to specify a few configurations in the configuration file to run
  3. Write your own business code

3.1 Automatic configuration principle:

What does SpringBoot configure for us in this scenario? Can you change it? What configurations can be modified? Can it be extended?

***AutoConfigruation: Help us to automatically configure the components in the container

***Properties: configuration class to encapsulate the contents of a configuration file

3.2 Static Resource Mapping Rules

  1. For all /webjars/**, go to classpath: meta-INF /resources/webjars/