Spring Boot @PropertySource: 从文件读取属性示例

假设我们有一个名为'application.properties'的属性文件,如果我们想在Spring Boot应用程序中使用'@PropertySource'注解来读取该文件,我们可以使用以下示例代码:

@Configuration
@PropertySource('classpath:application.properties')
public class AppConfig {
    
    @Value('${my.property}')
    private String myProperty;
    
    // getter and setter for myProperty
    
    // other configuration beans and methods
    
}

在上面的示例中,'@PropertySource'注解指定了'application.properties'文件的位置。使用'@Value'注解,我们可以将名为'my.property'的属性值注入到'myProperty'字段中。您可以根据需要添加其他属性和字段。

Spring Boot @PropertySource: 从文件读取属性示例

原文地址: https://www.cveoy.top/t/topic/nPfM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录