SpringBoot configuration related
1. Profile injection
_In SpringBoot you can inject a configuration file with four notes:
@ConfigurationProperties @Value @PropertySource @ImportResource
_1.@ConfigurationProperties usage
_Centered like this in my profile application.yml
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: puppy age: 12
JavaBean
/** - Map the value of each property configured in the configuration file to this component - @ConfigurationProperties: Tell SpringBoot to bind all the properties in this class to the related configurations in the configuration file; prefix = "person": Specify which prefix in the configuration file maps all the properties one by one - Only if this component is a component in a container can the @ConfigurationProperties function provided by the container be implemented. - Making it a component can be done in either of the following ways 1.@Component //If you add comments here, you do not need to add them when automatically configuring classes 2.@EnableConfigurationProperties(Person.class)Notes. */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; }
_2.@Value usage
@Value can only be read from application.yml
@Component @ConfigurationProperties(prefix = "person") @Validated public class Person { //lastName must be in mailbox format @Email @Value("${person.last-name}") private String lastName; @Value("#{11*2}") private Integer age; @Value("true") private Boolean boss;
_3.@ConfigurationProperties and @Value value comparison and use scenarios
_If we just need to get a value in the configuration file in some business logic, use @Value; _If we've specifically written a javaBean to map to a configuration file, we'll use @ConfigurationProperties directly;
_4.@PropertySource usage
@PropertySource: Load the specified configuration file (not application.yml); must be added @Component to have its spring managed _You need to specify prefix binding to JavaBean with @ConfigurationProperties(prefix ='")
* Only if this component is a component in a container can the container provide@ConfigurationProperties Functions; * @ConfigurationProperties(prefix = "person") Get the value from the global configuration file by default, that is, default can only be obtained from application.properties perhaps application.yml Get in; * */ @Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"}) @Data public class PersonConfig { private String lastName; private String firstName; }
_person.properties file
person.lastName=johnny persong.firstName=candy
5.@ImportResource
_Import Spring's profile xml format is placed on the main run class, Spring Boot does not have Spring's profile in it, we write our own profile, and we can't recognize it automatically
_For Spring's profile to take effect, load it; @ImportResource is marked on a configuration class
@ImportResource(locations = {"classpath:bean.xml"}) @SpringBootApplication public class SpringBoot01HelloworldQuickApplication { } bena.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean> </beans>
2. Profile Processor
_Role: The primary purpose of the Profile Processor is to prompt when writing a profile
_Mode 1 introduces the following dependencies in the pom.xml file:
<--Import the profile processor and you will be prompted to bind the profile--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Mode 2 checked when IDEA created the Spring-Boot project
Personal blogging system: https://www.askajohnny.com Welcome! This article is published by blog OpenWrite Release!