springboot 打包为jar后读取非jar文件中的外部配置文件并获取参数值
可以通过在Spring Boot应用程序中使用@PropertySource注解来读取外部配置文件中的属性值,并将其注入到Spring Boot应用程序中。
例如,在Spring Boot应用程序中,如果我们有一个外部配置文件config.properties,我们可以使用以下代码来读取该文件中的属性值:
@Configuration
@PropertySource("file:/path/to/config.properties")
public class AppConfig {
@Value("${my.property}")
private String myProperty;
// ...
}
在上面的代码中,@PropertySource注解将外部配置文件的路径指定为file:/path/to/config.properties,并将其加载到Spring Boot应用程序中。然后,我们使用@Value注解将my.property属性的值注入到myProperty字段中。
请注意,如果你将外部配置文件打包到jar文件中,则应该使用classpath前缀而不是file前缀来指定@PropertySource注解中的路径,例如:
@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {
// ...
}
``
原文地址: http://www.cveoy.top/t/topic/ealc 著作权归作者所有。请勿转载和采集!