将github信息注入Spring bean

时间:2019-06-18 13:06:03

标签: spring-boot spring-boot-actuator

Am正在尝试从git hub获得值,其中它有一个名为abc.properties的属性文件。我需要从属性文件中读取值并将其注入到bean类变量中

1 个答案:

答案 0 :(得分:0)

您可以使用@PropertySource@Configuration的组合将任何本地属性文件绑定到bean的字段中。

例如:

@Configuration
@PropertySource("file:/path/to/abc.properties")
public class AbcProperties {

    private String someProperty;
    private int anotherProperty;

    // standard getters and setters
}

这假设文件位于本地。请注意,@PropertySource也可以引用类路径相对文件,如果它们在您的项目中。

如果您确实想在运行时从GitHub远程读取文件,则可能需要自定义PropertySourcesPlaceholderConfigurer或考虑使用Spring Cloud Config来管理外部配置。

相关问题