How to Read application.yml Properties with SpringBoot @ConfigurationProperties
This guide shows how to externalize fixed settings like file upload paths in a SpringBoot backend by defining them in application.yml, creating a @ConfigurationProperties‑annotated config class with matching fields, and accessing the values via static methods or bean injection.
Scenario
In a SpringBoot backend project, certain fixed properties—such as the server file‑upload directory—need to be placed in the application.yml configuration file.
Implementation
First add the properties under a root element (here ruoyi) in application.yml:
ruoyi:
name: RuoYi
version: 2.3.0
copyrightYear: 2019
demoEnabled: true
profile: D:/ruoyi/uploadPathThen create a configuration class in a new config package. Annotate the class with @Component and @ConfigurationProperties(prefix = "ruoyi"). The class fields must have the same names as the keys under the ruoyi node.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Reads project‑related configuration.
*/
@Component
@ConfigurationProperties(prefix = "ruoyi")
public class RuoYiConfig {
/** 项目名称 */
private String name;
/** 版本 */
private String version;
/** 版权年份 */
private String copyrightYear;
/** 实例演示开关 */
private boolean demoEnabled;
/** 上传路径 */
private static String profile;
// getters and setters for the above fields
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getVersion() { return version; }
public void setVersion(String version) { this.version = version; }
public String getCopyrightYear() { return copyrightYear; }
public void setCopyrightYear(String copyrightYear) { this.copyrightYear = copyrightYear; }
public boolean isDemoEnabled() { return demoEnabled; }
public void setDemoEnabled(boolean demoEnabled) { this.demoEnabled = demoEnabled; }
public static String getProfile() { return profile; }
public void setProfile(String profile) { RuoYiConfig.profile = profile; }
/**
* Retrieves the upload path.
*/
public static String getUploadPath() {
return getProfile();
}
}The prefix value must match the root element defined in application.yml. The static profile field stores the upload directory, and the static method getUploadPath() returns it.
Usage examples:
Direct static call: RuoYiConfig.getUploadPath(); returns D:/ruoyi/uploadPath.
Bean injection: @Autowired private RuoYiConfig ruoYiConfig; then ruoYiConfig.getProfile(); obtains the same value.
Both approaches provide convenient access to the configuration property throughout the application.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
