-
读取主配置文件(application.yml/application.properties)
-
@Value注解
@Value("${server.port}") public String port;
-
@ConfigurationProperties注解(需要为属性提供set方法)
@Component //指定实体类与配置项映射的前缀 @ConfigurationProperties(prefix = "server") public class IServer { public String port; public void setPort(String port) { this.port = port; } }
-
-
读取自定义配置文件
-
@PropertySource + @Value
//指定resources目录下的自定义配置文件(可指定多个) @PropertySource(value = {"classpath:weather.properties"}) @Value("${weather.url}") String weatherUrl;
-
@PropertySource + @ConfigurationProperties(需要为属性提供set方法)
@Component @PropertySource(value = {"classpath:weather.properties"}) @ConfigurationProperties(prefix = "weather") public class Weather { public String url; public void setUrl(String url) { this.url = url; } }
-
SpringBoot读取配置文件
最新推荐文章于 2024-07-31 15:55:27 发布