如果不想使用默认的application.properties
,而想将属性文件放到jar包外面,可以使用如下两种方法:
涿鹿网站建设公司创新互联建站,涿鹿网站设计制作,有大型网站制作公司丰富经验。已为涿鹿上1000+提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的涿鹿做网站的公司定做!
只能设置全路径。因为Java -jar
运行jar包时,无法指定classpath(无论通过参数还是环境变量,设置的classpath都会被覆盖)。
方法1:命令行传参指定spring.config.location
java -jar -Dspring.config.location=D:\zTest\config\config1.properties springbootrestdemo-0.0.1-SNAPSHOT.jar
还可以用spring.config.location
指定路径,这样会在这个路径中去寻找application-{profile}.properties
。
还可以用spring.config.location
指定路径,然后用spring.config.name
指定配置文件名字。
可以用逗号隔开,指定多个路径和名字
方法2:使用@PropertySource注解。
@SpringBootApplication @PropertySource(value={"file:D:/zTest/config/config1.properties"}) public class SpringbootrestdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootrestdemoApplication.class, args); } }
下面看下Spring Boot 配置文件和日志文件放到jar之外
1.设置打包jar的时候排除文件
<resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>*.properties</exclude> <exclude>logback.xml</exclude> </excludes> </resource> </resources>
2.启动的时候传入参数指定位置
java -jar xxx.jar --spring.config.location=D:\springconfig\ --logging.config=D:\springconfig\logback.xml
springboot 默认找配置文件的位置如下
// Note the order is from least to most specific (last one wins) private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:
总结
以上所述是小编给大家介绍的Spring Boot 把配置文件和日志文件放到jar外部,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
名称栏目:SpringBoot把配置文件和日志文件放到jar外部
当前URL:https://www.cdcxhl.com/article44/ipsshe.html
成都网站建设公司_创新互联,为您提供定制开发、网站设计公司、小程序开发、用户体验、企业网站制作、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联