springboot启动时加载外部配置文件的方法-创新互联
前言
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了施甸免费建站欢迎大家使用!相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。本文主要给大家介绍了关于spring boot启动时加载外部配置文件的相关内容,下面话不多说了,来随着小编一起学习学习吧。
业务需求:
加载外部配置文件,部署时更改比较方便。
先上代码:
@SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class); springApplicationBuilder.web(true); Properties properties = getProperties(); StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addLast(new PropertiesPropertySource("micro-service", properties)); springApplicationBuilder.environment(environment); springApplicationBuilder.run(args); } private static Properties getProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); propertiesFactoryBean.setIgnoreResourceNotFound(true); Resource fileSystemResource = resolver.getResource("file:/opt/company/test.properties"); propertiesFactoryBean.setLocations(fileSystemResource); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); } }
分享标题:springboot启动时加载外部配置文件的方法-创新互联
路径分享:http://scjbc.cn/article/depjoc.html