温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot 把配置文件和日志文件放到jar外部

发布时间:2020-09-25 11:49:36 来源:脚本之家 阅读:281 作者:mrr 栏目:编程语言

如果不想使用默认的application.properties,而想将属性文件放到jar包外面,可以使用如下两种方法:

只能设置全路径。因为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外部,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI