SoFunction
Updated on 2025-03-03

Several ways to read configuration file attribute values

existSpring BootIn 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

@ValueAnnotations 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@ControllerIn the class marked with annotation,@ValueAnnotation 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

@ConfigurationPropertiesAnnotations 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 definedappProperties of prefix:

=MyApp
=1.0.0
=A sample application

You can create a configuration class and use@ConfigurationPropertiesThe annotation willappThe 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 injectedAppConfigPropertiesClass 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@ConfigurationPropertiesThe 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

EnvironmentThe 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);
    }
}

NoticeEnvironmentThe 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@PropertySourceAnnotation loads the properties in it.

Example

  • Create a custom configuration file, the content is as follows:
custom.property1=value1
custom.property2=value2
  • use@PropertySourceAnnotation 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 sureThe file is located atclasspathNext (such assrc/main/resourcesTable 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!