existSpring Boot
In the project, you can read it in the following waysConfiguration attribute values in the file:
1. Use the @Value annotation to read a single attribute value
@Value
Annotations can be used directly for injectionAttribute value in the file.
Example
Assume thatThe following configuration items are defined in :
=MyApp =1.0.0
Can be at any@Component
、@Service
、@Controller
In the class marked with annotation,@Value
Annotation gets these values:
import ; import ; @Component public class AppConfig { @Value("${}") private String appName; @Value("${}") private String appVersion; public void printConfig() { ("App Name: " + appName); ("App Version: " + appVersion); } }
Notice:
${}
Indicates reading from the configuration fileAttribute value.
2. Use the @ConfigurationProperties annotation to read the configuration prefix
@ConfigurationProperties
Annotations allow batch reading of properties with the same prefix and injecting them into a Java class. This method is suitable for managing a large number of configuration items.
Example
Assume thatA group of defined
app
Properties of prefix:
=MyApp =1.0.0 =A sample application
You can create a configuration class and use@ConfigurationProperties
The annotation willapp
The prefix attributes are injected into it:
import ; import ; @Component @ConfigurationProperties(prefix = "app") public class AppConfigProperties { private String name; private String version; private String description; // Getter and Setter methods public String getName() { return name; } public void setName(String name) { = name; } public String getVersion() { return version; } public void setVersion(String version) { = version; } public String getDescription() { return description; } public void setDescription(String description) { = description; } }
In other classes, it can be injectedAppConfigProperties
Class to get configuration values:
import ; import ; @Service public class AppService { private final AppConfigProperties appConfigProperties; @Autowired public AppService(AppConfigProperties appConfigProperties) { = appConfigProperties; } public void printAppInfo() { ("App Name: " + ()); ("App Version: " + ()); ("App Description: " + ()); } }
Notice:
@ConfigurationProperties
The annotation class must be one@Component
, and need to beEnable property binding support in , which is usually enabled by default in Spring Boot.
3. Use Environment to read properties
Environment
The interface provides a way to dynamically obtain attribute values, suitable for reading attributes in configuration files at runtime.
Example
import ; import ; import ; @Component public class EnvironmentConfig { private final Environment environment; @Autowired public EnvironmentConfig(Environment environment) { = environment; } public void printEnvConfig() { String appName = (""); String appVersion = (""); ("App Name: " + appName); ("App Version: " + appVersion); } }
Notice:
Environment
The interface is suitable for use in scenarios where dynamically obtaining configurations or configuration items are uncertain.
4. Use @PropertySource to load custom configuration files
Apart from, you can also define custom configuration files and use
@PropertySource
Annotation loads the properties in it.
Example
- Create a custom configuration file
, the content is as follows:
custom.property1=value1 custom.property2=value2
- use
@PropertySource
Annotation loads custom configuration files:
import ; import ; import ; @Configuration @PropertySource("classpath:") public class CustomConfig { @Value("${custom.property1}") private String property1; @Value("${custom.property2}") private String property2; public void printCustomConfig() { ("Custom Property 1: " + property1); ("Custom Property 2: " + property2); } }
Notice:make sure
The file is located at
classpath
Next (such assrc/main/resources
Table of contents).
Summarize
The above are several common ways to read file attribute values in Spring Boot:
- @Value Annotation: Read a single attribute value directly.
- @ConfigurationProperties Annotation: Batch reading of properties with the same prefix.
- Environment interface: Dynamically read attributes, suitable for runtime acquisition.
- @PropertySource Annotation: Load the property value of the custom configuration file.
The above is the detailed content of several ways SpringBoot3 can read the attribute value of configuration file. For more information about SpringBoot3 can read the attribute value of attribute value, please pay attention to my other related articles!