简体   繁体   English

如何配置Spring以从jar外部加载application.properties?

[英]How to configure Spring to load application.properties from outside of jar?

I'd like to have a directory structure as so: 我想要这样的目录结构:

/Directory
    - Application.jar
    - application.properties

So that I can change properties without having to repackage and redeploy (and instead just restarting the jar). 这样我就可以更改属性,而不必重新打包和重新部署(而只需重新启动jar)。 How can I accomplish this with spring annotations or configuration classes? 如何使用spring注释或配置类完成此操作?

I'm not asking about making external resources available with my web application, I'm also looking to change the location from where spring loads the application.properties file. 我并不是在问如何使外部资源可用于我的Web应用程序,我还想更改Spring从中加载application.properties文件的位置。

You're mentioning jar, so you're using Spring Boot? 您提到的是jar,所以您正在使用Spring Boot?

If so, external application.properties in the same directory (structure just like you described) will override application.properties packaged inside the jar file. 如果是这样,同一目录(结构如您所述)中的外部application.properties将覆盖jar文件中打包的application.properties。

Then, if you have something like key=value in your application.properties, you can inject it in your code with @Value("${key}") String key . 然后,如果application.properties中有类似key=value ,则可以使用@Value("${key}") String key将其注入代码中。

Try it, it will just work :) 试试看,它会工作的:)

You may want to explore PropertyPlaceholderExplorer class in Spring. 您可能要在Spring中探索PropertyPlaceholderExplorer类。 This class provides the facility to access the properties file external to your jar/war bundle. 此类提供了访问jar / war捆绑包外部的属性文件的工具。 There is a short nice tutorial on this as well here . 在灯架上有一个简短的教程不错,以及在这里

If you start using Spring Boot (at some stage you for sure will). 如果您开始使用Spring Boot(肯定会在某个阶段使用)。 you get powerful configuration externalization features . 您将获得强大的配置外部化功能

With Spring Boot your applications.properties are automatically loaded into Spring Boot context and you can use ${...} placeholders. 使用Spring Boot,您的applications.properties将自动加载到Spring Boot上下文中,并且您可以使用${...}占位符。

You can use even more modern feature @ConfigurationProperties to map you configuration to POJO. 您可以使用更现代的功能@ConfigurationProperties将您的配置映射到POJO。 This POJO can even be validated by Java EE validation annotations (eg @NotNull) 该POJO甚至可以通过Java EE验证注释(例如@NotNull)进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar 如何在 spring 框架中加载 application.properties? - How to load application.properties in spring framework? Spring-如何从application.properties中的spring.datasource配置OracleDataSource - Spring - How to configure OracleDataSource from spring.datasource in application.properties 在Spring Boot中从application.properties配置国际化 - Configure internationalization from application.properties in spring boot 如何从 spring 引导应用程序之外的位置读取 application.properties 文件 - How to read the application.properties file from a location outside the spring boot application Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 从 Spring Boot 中的 application.properties 加载动态对象 - Load dynamic objects from application.properties in Spring Boot 无法在Spring Boot服务中从application.properties加载值 - Cannot load values from application.properties in a spring boot service 如何在 Mockito 测试中从 application.properties 加载属性 - How to load the properties from application.properties in Mockito test Spring 引导不加载 application.properties - Spring Boot does not load application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM