Cutting edge is introduced

In SpringBoot, annotations to read configuration file properties are:

  • @Value
  • @PropertySource
  • @ConfigurationProperties

Next, we carry out a simple case introduction:

@Value

Function: @value first reads the properties defined in the default configuration file application.properties.

Example: @value(“${user.name}”)

Application. Properties

user.username=csp
user.password=123
user.age=22
Copy the code

Java code read:

@Data// Lombok plugin annotations, which can be replaced with setter/getter
@AllArgsConstructor// Lombok plug-in annotations, which can be replaced with the full-parameter constructor
@NoArgsConstructor// Lombok plug-in annotations, which can be replaced with an empty parameter constructor
@Component
public class User {
    @Value("${user.username}")
    private String username;//csp
    @Value("${user.password}")
    private String password;/ / 123
    @Value("${user.age}")
    private int age;/ / 22
}
Copy the code

@PropertySource

Function: @propertysource reads properties defined in the configuration file taken from the definition.

Method of use: @ PropertySource (value = {classpath: / specify the configuration file name}) the annotations on the loading member variables, for example: @ PropertySource (value = {} “the classpath: / user. The properties”)

User.properties:

user.username=csp1999
user.password=123456
user.age=22
Copy the code

Java code:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@PropertySource(value={"classpath:/user.properties"})
public class User {
    @Value("${user.username}")
    private String username;//csp1999
    @Value("${user.password}")
    private String password;/ / 123456
    @Value("${user.age}")
    private int age;/ / 22
}
Copy the code

@ConfigurationProperties

Function: @ConfigurationProperties Reads properties defined in the default configuration file, but can specify property prefixes.

ConfigurationProperties(prefix = “user”) : @configurationProperties (prefix = “user”)

The default configuration file the application. The properties/application. In the yml configuration:

user.username=csp789
user.password=456
user.age=18
Copy the code

Note: This annotation will appear in the IDEA window in new versions of SpringBoot



This hint, solution 1:

<! Personal version: Without this dependency, my version of Spring Boot will tell you something is wrong when using the @configurationProperties (prefix = "XXX ") annotation in the configuration class -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artif
    <optional>true</optional>
</dependency>
Copy the code

Solution 2:

Java code:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "user")
@Component
public class User {
	// With the prefix, you can omit the @value annotation
    private String username;//csp789
    private String password;/ / 456
    private int age;/ / 18
}
Copy the code

@ ConfigurationProperties and @ EnableConfigurationProperties cooperate to use

Using @ConfigurationProperties alone

@Data
@Component // Add this annotation to give DemoConfig to IOC management
@ConfigurationProperties(prefix = "csp.user")
public class DemoConfig {
	private String username;
	private String password;
}
Copy the code

In this case, you need to use DemoConfig directly:

@Autowired
private DemoConfig demoConfig;
Copy the code

Can!

@ ConfigurationProperties and @ EnableConfigurationProperties cooperate to use

@Data
// @component // DemoConfig1 is not assigned to IOC management
@ConfigurationProperties(prefix = "csp.user2")
public class DemoConfig1 {
	private String username1;
	private String password1;
}

@Data
// @component // DemoConfig2 is not managed by IOC
@ConfigurationProperties2(prefix = "csp.user2")
public class DemoConfig2 {
	private String username2;
	private String password2;
}
Copy the code

In this case, DemoConfig1 and DemoConfig2 are needed

@Service // @configuration /@Componet/@Controller/ @repository just need IOC management
@EnableConfigurationProperties({ DemoConfig1.class, DemoConfig2.class })
public class TestService{
	@Autowired
	private DemoConfig1 demoConfig1;

	@Autowired
	private DemoConfig2 demoConfig2;
}
Copy the code

@propertysource and @ConfigurationProperties work together

@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "user")
@Component
@PropertySource(value={"classpath:/user.properties"})
public class User {
    private String username;//csp1999
    private String password;/ / 123456
    private int age;/ / 22
}
Copy the code

If the article is helpful to you, please also one key three connect!