加载自定义的配置文件
2018-01-26 00:00
288 查看
加载自定义的properties文件<br>
remote.propertiesremote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource/pic/
方式一
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) @PropertySource("classpath:remote.properties") @Component public class RemoteProperties { private String uploadFilesUrl; private String uploadPicUrl; }
@RestController public class TestService{ @Autowired RemoteProperties remoteProperties; public void test(){ String str = remoteProperties.getUploadFilesUrl(); System.out.println(str); } }
方式二
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) @PropertySource("classpath:remote.properties") @Configuration public class RemoteProperties { private String uploadFilesUrl; private String uploadPicUrl; }
@RestController public class TestService{ @Autowired RemoteProperties remoteProperties; public void test(){ String str = remoteProperties.getUploadFilesUrl(); System.out.println(str); } }
方式三
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) @PropertySource("classpath:remote.properties") public class RemoteProperties { private String uploadFilesUrl; private String uploadPicUrl; }
@EnableConfigurationProperties(RemoteProperties.class)
@RestController public class TestService{ @Autowired RemoteProperties remoteProperties; public void test(){ String str = remoteProperties.getUploadFilesUrl(); System.out.println(str); } }
方式四
@PropertySource("classpath:remote.properties") @Configuration public class RemoteProperties { @Value("${remote.uploadFilesUrl}") private String uploadFilesUrl; @Value("${remote.uploadPicUrl}") private String uploadPicUrl; @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
@RestController public class TestService{ @Autowired RemoteProperties remoteProperties; public void test(){ String str = remoteProperties.getUploadFilesUrl(); System.out.println(str); } }
该方式一般只用于
SpringMVC中<br>
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false)该注解用于绑定属性。prefix用来选择属性的前缀,也就是在remote.properties文件中的"remote",ignoreUnknownFields是用来告诉SpringBoot在有属性不能匹配到声明的域时抛出异常。<br>
@PropertySource("classpath:remote.properties")配置文件路径<br>
加载自定义的xml文件
@Configuration @ImportResource(locations={"classpath:application-bean.xml"}) publicclass ConfigClass { }
相关文章推荐
- C#读取配置文件详解 完全自定义实现动态加载dll
- springmvc系列(二)-自定义dispatcherservlet加载的xml配置文件
- 程序自定义加载log4j配置文件
- 实现自定义配置文件加载
- yii2 配置文件加载顺序, 以及调用自定义配置信息。
- 如何加载和使用配置文件中的自定义实体属性
- 在Thinkphp中【自动加载自定义扩展配置文件】!
- 自定义加载Spring配置文件
- Spring 加载自定义 porperties 文件配置
- 加载自定义目录下的springmvc.xml配置文件 (2)
- ThinkPHP加载自定义的外部文件和配置文件
- 谷歌验证码非web.xml非servlet拦截 使用配置文件自定义加载,防止多次点击(防js攻击)
- ActiveMQ 配置自定义启动文件和加载扩展包(以及debug模式监控 for eclipse)
- web.xml加载自定义配置文件(.xml)
- 【SpringMVC】加载自定义目录下的springmvc.xml配置文件(三)
- log4j加载自定义的日志的配置文件
- 自定义配置文件的使用及加载-txt
- spring-boot 如何加载rsources下面的自定义配置文件
- 如何在Spring容器中加载自定义的配置文件
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置