1. Preface
In Spring Boot projects, you usually need toor
Read configuration information from the configuration file.
@Value
Annotations provide an easy way to inject configuration values into Spring components.
2. Introduction to @Value Annotation
@Value
Is annotation provided by Spring to read values from configuration files or other sources (such as system environment variables, SpEL expressions) and inject them into fields or method parameters of the bean. The basic syntax is as follows:
@Value("${}") private String propertyValue;
3. Common usage of @Value annotation
1. Read or configure the value
(1) Configuration file example
=Spring Boot Demo =1.0.0
app: name: Spring Boot Demo version: 1.0.0
(2) Java code example
@Component public class AppConfig { @Value("${}") private String appName; @Value("${}") private String appVersion; public void printConfig() { ("Application Name: " + appName); ("Application Version: " + appVersion); } }
(3) Test output
Application Name: Spring Boot Demo
Application Version: 1.0.0
2. Use @Value to set the default value
If no attribute is defined in the configuration file,@Value
Annotations can provide a default value. The default syntax is:${defaultValue}
:
@Component public class DefaultValueExample { @Value("${:Unknown Author}") private String author; public void printAuthor() { ("Application Author: " + author); } }
ifor
Not configured
, then output:
Application Author: Unknown Author
3. Read system environment variables and Java runtime parameters
@Value
It can also be used to read system environment variables or-D
JVM running parameters passed in the method:
@Component public class EnvConfig { @Value("${JAVA_HOME}") private String javaHome; @Value("${}") private String userName; public void printSystemProperties() { ("JAVA_HOME: " + javaHome); ("User Name: " + userName); } }
If the running environment variable is setJAVA_HOME=/usr/lib/jvm/java-11-openjdk
, then output:
JAVA_HOME: /usr/lib/jvm/java-11-openjdk
User Name: admin
4. Combined with Spring Expression Language (SpEL)
@Value
Annotations support the Spring Expression Language (SpEL) and can be used to calculate values dynamically.
(1) Basic SpEL expression
@Component public class SpELExample { @Value("#{2 * 5}") private int result; public void printResult() { ("Result: " + result); } }
Output:
Result: 10
(2) Quote the properties of the Bean
@Component public class AnotherBean { private String message = "Hello from AnotherBean"; public String getMessage() { return message; } } @Component public class BeanReferenceExample { @Autowired private AnotherBean anotherBean; @Value("#{}") private String messageFromAnotherBean; public void printMessage() { (messageFromAnotherBean); } }
Output:
Hello from AnotherBean
5. Read array, collection, and Map type data
(1) Read the array
=192.168.1.1,192.168.1.2,192.168.1.3
@Component public class ArrayConfig { @Value("${}") private String[] servers; public void printServers() { ((servers)); } }
Output:
[192.168.1.1, 192.168.1.2, 192.168.1.3]
(2) ReadList
@Component public class ListConfig { @Value("#{'${}'.split(',')}") private List<String> serverList; public void printServerList() { (serverList); } }
Output:
[192.168.1.1, 192.168.1.2, 192.168.1.3]
(3) ReadMap
=username:admin,password:123456,url:jdbc:mysql://localhost:3306/test
@Component public class MapConfig { @Value("#{${}}") private Map<String, String> dbConfig; public void printDbConfig() { (dbConfig); } }
Output:
{username=admin, password=123456, url=jdbc:mysql://localhost:3306/test}
4. Comparison between @Value and @ConfigurationProperties
characteristic | @Value | @ConfigurationProperties |
---|---|---|
Range of action | Suitable for single value injection | Suitable for the entire configuration object mapping |
Supported data types | Mainly used for String, basic types, arrays | Can bind complex objects (such as List, Map, Custom objects) |
Code simplicity | Suitable for a small number of configuration parameters | Suitable for a large number of configuration parameters |
Whether SpEL is supported | support | Not supported |
Example:@ConfigurationProperties
usage
=Spring Boot Demo =1.0.0
@Configuration @ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private String version; // getter & setter }
5. Summary
- The @Value annotation is used to inject values from the configuration file into Spring components.
- @Value can be used to read values in or , and supports default values.
- @Value can also be used to read environment variables, JVM running parameters, and SpEL expressions.
- @Value can parse arrays, Lists, Map and other data structures.
- @ConfigurationProperties is recommended for complex configurations, while @Value is suitable for simple value injection.
This is the end of this article about the detailed explanation of the use examples of @Value annotation in Spring Boot. For more related contents of using @Value annotation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!