SpringBoot series learning summary and extension


@[toc]

preface

  • I recommend introducing the Lombok package so that entity classes don’t have to write their own get, set, toString, and other methods every time
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.1610.</version>
		</dependency>
Copy the code
  • With the @data annotation, the compiler automatically adds methods like get and set, which greatly reduces the amount of code and makes it look much more comfortable

One, the application properties

  • Grammatical structure:
    • key=value

  • Function:

    • Change the default SpringBoot auto-configuration, because SpringBoot is automatically configured for us at the bottom;
  • Contrast tradition:

<server>
    <port>8888<port>
</server>
Copy the code
  • Contrast now: much simpler.

Second, the application. Yml

  • The official recommendation is to use YML because it is simpler and more powerful.

  • Syntax format: key: space value

    • Spaces cannot be omitted
    • Indent the hierarchy so that any column aligned to the left is of the same hierarchy.
    • Properties and values are case-sensitive

  • Yml writes to an array: both of the following are legal

  • Yml writes objects: both of the following are legal

3. Yml injection configuration file

3.1 @value Injection (old version)

  • Entity class
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@Component
public class Person {
    @value (" Xiaoding Xiaoxiao ")
    private String name;
    @Value("24")
    private Integer age;
    @Value("175")
    private String high;
    @ Value (" fishing ")
    private String like;
}
Copy the code
  • Unit test execution results

  • disadvantages
    • If the class is particularly complex, it can be cumbersome to write.

3.2. Yml injection

  • Create a new entity class and annotate it with @data
import lombok.Data;

@Data
public class Student {
    private String name;
    private Integer age;
    private String high;
    private String like;
}
Copy the code
  • ② Write the YML configuration file:
Server: port: 7777 student: name: large size large age: 23 high: 173 like: fishingCopy the code
  • Add @Component, @ConfigurationProperties(prefix = “Student”) annotation to entity class
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private Integer age;
    private String high;
    private String like;
}
Copy the code
- Note: it will be red, but does not affect the execution of the program, we can remove the red. Click on the Open DocumentationCopy the code

You can also click here Pom file to add this can be introduced.

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
Copy the code
  • The perfect solution

  • Write unit tests and inject entity classes
import com.dyjTest.dyjProject.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DyjProjectApplicationTests {

	@Autowired(required = false)
	Student student;

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

The result is as follows:The code! Solve the TA!Execute again: The result is as followsPerfect solution!!

  • conclusion

@propertysource Loads the specified configuration file

  • PropertySource: Loads the specified configuration file

  • ConfigurationProperties: Gets the value from the global configuration file by default;

  • (1) a new person. Yml

  • (2) the entity class
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@PropertySource("classpath:person.yml")
public class Person {
    @Value("${name}")
    private String name;
    private Integer age;
    private String high;
    private String like;
}
Copy the code
  • ③ The unit test results are as follows:

5. Configuration file placeholders

  • Placeholders generate random numbers
The ${random value}, ${random.int}, ${random.long}
${random.int(10)}, ${random.int[1024.65536]}
Copy the code

Priority of multiple YML configuration files

  • If you have both properties and YML profiles, they complement each other if they do not conflict, whereas properties dominates.
  • It can be placed in the following four places

  • ① In the root directory
  • ② In the config directory of the root directory
  • ③ Resources = config
  • (4) the resources directory

  • Perform the following to see the priority order

① Simultaneous existence: the implementation isConfiguration files in the config directory of the root directory

② Remove the premise of ① : execute isConfiguration file in the root directory

③ Remove ①② premise: execute isConfig file in the Resources directory

④ Remove the premise of ①② 3: will be executedThe resources directoryThat’s where the default is

  • Summary order: ②>①>③>④
    • Config files in the root directory > Config files in the root directory > Config files in the resources directory > Resources (default location)

Vii. Use of multiple environment configuration files during development

7.1. Specify the Configuration file

  • In real development, we might have many sets of environments, such as development environment (DEV), gray environment (PRE), and online environment (PROD), and we would need multiple configuration files to manage the configuration information for these environments.
  • Yml supports multiple environment configurations as follows:
  • In the master profile, you can specify which profile to start the project from by
Spring: Profiles: active: indicates the environment nameCopy the code
  • Give it a try:
    • Specify the dev

– specify the prod

Is YML convenient? Greatly save our workload ~~

7.2 How to automatically select configuration files in actual development

  • In the actual work, we can not say manual to specify the configuration file, or every time the package has to specify, how troublesome oh ~
  • Let’s optimize the above method in combination with pom.xml and let TA identify the environment we want to package
  • ① Added poM file packaging option
<! Package selection environment -->
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env>dev</env>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>pre</id>
			<properties>
				<env>pre</env>
			</properties>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<env>prod</env>
			</properties>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
		</profile>
	</profiles>
Copy the code

Refresh Maven and you can see that our Maven structure has changed:

  • (2) Specify the master configuration file as a placeholder
spring:
  profiles:
    active: '@env@'
Copy the code
  • ③ Select the designated environment to start the project: perfect to meet our needs!!

7.3 yML Single file With Multiple Configurations

  • Yml can also implement multiple configurations in a single file
  • The syntax is as follows
server:
  port: 6666
---
server:
  port: 7777
---
server:
  port: 8888
Copy the code
  • Properties does not support this notation

This is the end of the application profile gameplay


I see a long way to go, I’ll keep searching. If you think I’m a good blogger! Writing is not easy, please like, pay attention, comment to the blogger an encouragement bar ~ reprint please indicate the source oh ~