Sobes.tech
Junior — Middle

How is configuration management implemented through configuration files in the Spring framework?

sobes.tech AI

Answer from AI

In Spring, configuration management through configuration files is usually done using properties or YAML format files (application.properties or application.yml). These files specify configuration parameters such as database connection settings, ports, paths, and other parameters.

Spring automatically loads these files at application startup and provides access to the values through annotations, for example, @Value or via the Environment bean.

Example of usage in a class:

@Value("${server.port}")
private int serverPort;

You can also use a class annotated with @ConfigurationProperties to group related settings:

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private int timeout;
    // getters and setters
}

To activate this functionality, add @EnableConfigurationProperties(AppProperties.class) in the configuration class.

Thus, configuration files allow centralized management of application parameters without changing the code.