This is the 24th day of my participation in the August More Text Challenge

The configuration file

There are two types of springBoot configuration files

properties

Syntax key = value

service.port = 8081

If you use properties and class binding

/ / the specified configuration file @ PropertySource (value = "classpath: student. The properties") public class student {/ / use the properties file set properties @Value("${student.name}") private String name;Copy the code

yaml

Syntax structure: key: space value

Spaces are very strict, vertically aligned, and attributes need to be indented

service:   port: 8081

object

Student: name: YY age: 22 student1: {name: YY,age: 22}

An array of

An array of pets:

  • cat
  • dog
  • pig

​ pets1: [cat,dog,pig]

Attribute assignment

pojo

// This class is component@component // maps every property of the configuration file to this Component, Set @configurationProperties (prefix = "student") public class student {private String name; private int age;Copy the code

test

@SpringBootTest class DemoApplicationTests { @Autowired Student student; @Test void contextLoads() { System.out.println(student.getAge()); }}Copy the code

Loosely bound

The database field name last-name can be automatically bound to the hump nomenclature lastName

private String LastName;

JSR303 data verification

The @validate data verification must be enabled first

// Data verification

@age public class Student {@email (message = "📪error") private String name; }Copy the code

Environment with more

priority

Specify the environment

Multiple files

Spring: Profiles: Active: test -- Server: port: 8083 Spring: Profiles: test -- server: port: 8082 spring: profiles: work --- server: port: 8084 spring: profiles: upCopy the code

error

  1. Writing server as Service causes port changes not to take effect
  2. If multiple environments are configured in a single file, the file name must be

The log

The use of log4j

1. Rely on

All the logging in the starter needs to be removed, just remove one of them and put it first.

<dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <exclusions>
               <exclusion>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-logging</artifactId>
               </exclusion>
           </exclusions>
Copy the code

2. The configuration XML

<? The XML version = "1.0" encoding = "utf-8"? > <Configuration status="INFO" monitorInterval="1800"> <appenders> <Console name="consolePrint" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" /> </Console> </appenders> <loggers> <! <logger name="com.wocai.keyun.dao.bus" level="info" additivity="false" ref="consolePrint"/> </logger> <root level="info"> <appender-ref ref="consolePrint" /> </root> </loggers> </Configuration>Copy the code

3. Configure the path

The PATH of the XML file that needs to be specified for logging, and the path of the DAO layer interface

logging.config=classpath:log4j2.xml logging.level.com.wocai.keyun.dao.bus = info mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl