如何在SpringBoot下读取自定义properties配置文件
2017-07-28 01:06
856 查看
本文欢迎转载,转载请注明出处,谢谢~(作者:喝酒不骑马 Colton_Null)
from CSDN
其中
四、如何使用?
在想要使用配置文件的方法所在类上表上注解
并自动注入
在方法中使用
这里str就是配置文件中的”/resource/files/”了。
from CSDN
如何在SpringBoot下读取自定义properties配置文件?
SpringBoot工程默认读取application.properties配置文件。如果需要自定义properties文件,如何读取呢?一、在resource中新建.properties文件
在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下。如图remote.properties所示二、编写配置文件
remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource/pic/
三、新建一个配置类RemoteProperties.java
@Configuration @ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) @PropertySource("classpath:config/remote.properties") @Data @Component public class RemoteProperties { private String uploadFilesUrl; private String uploadPicUrl; }
其中
@Configuration表明这是一个配置类
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)该注解用于绑定属性。prefix用来选择属性的前缀,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用来告诉SpringBoot在有属性不能匹配到声明的域时抛出异常。
@PropertySource("classpath:config/remote.properties")配置文件路径
@Data这个是一个lombok注解,用于生成getter&setter方法,详情请查阅lombok相关资料
@Component标识为Bean
四、如何使用?
在想要使用配置文件的方法所在类上表上注解
EnableConfigurationProperties(RemoteProperties.class)
并自动注入
@Autowired RemoteProperties remoteProperties;
在方法中使用
remoteProperties.getUploadFilesUrl()就可以拿到配置内容了。
@EnableConfigurationProperties(RemoteProperties.class)
@RestController
public class TestService{
@Autowired RemoteProperties remoteProperties;
public void test(){
String str = remoteProperties.getUploadFilesUrl();
System.out.println(str);
}
}
这里str就是配置文件中的”/resource/files/”了。
相关文章推荐
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- 在SpringBoot下读取自定义properties配置文件的方法
- 如何配置properties文件和读取properties文件参数
- 如何在spring中读取properties配置文件里面的信息
- 如何在spring中读取properties配置文件里面的信息
- springboot读取自定义配置文件