Spring Boot allows you to externalize your configuration so that you can use the same application code in different environments that you can usepropertiesFiles,YAMLFiles, environment variables, and command-line arguments are used to externalize the configuration@Value
Annotations are injected directly into your bean, via Spring’sEnvironment
Abstract access, or through@ConfigurationProperties
Bind to structured objects. So how do you do Spring Boot to inject external configuration into static variables inside your application? The operation is as follows:
Property configuration classStaticProperties.class
@Component public class StaticProperties { public static String CUSTOM_NAME; @Value("${custom.name}") public void setCustomName(String customName) { CUSTOM_NAME = customName; }}Copy the code
Spring Boot configuration promptresources/META-INF/spring-configuration-metadata.json
{
"properties": [
{
"name": "custom.name",
"type": "java.lang.String",
"sourceType": "com.anoyi.xxx.config.StaticProperties"
}
]
}Copy the code
Spring the Boot configurationapplication.properties
custom.name=anoyi
Copy the code
At this point, you can reference staticProperties.custom_name globally in Spring Boot
© copyright belongs to the author, reprint or content cooperation please contact the author
● Convert HTML to PDF new pose
● Java calls the Docker API using UnixSocket
● Fastjson fatal flaw
● Service mesh-GRPC Local integrated remote Service
● Use Thymeleaf to render HTML dynamically
● Fastjson fatal flaw
● Spring Boot 2 integrates log4J2 logging framework
● Java interview key points summary set of core reference answers
● Java interview key points summary of the framework of reference answers
● How to protect your password
● Spring Boot RabbitMQ – Priority queue
This article is published by OpenWrite!