Spring Boot 读取基于类路径 arg 的属性文件 [英] Spring Boot Reading Properties Files Based on classpath arg

查看:39
本文介绍了Spring Boot 读取基于类路径 arg 的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个独立的 boot.jar,我需要开始将它集成到我们的更高环境中.每个环境都有一个属性文件,其中包含数据库特定的连接信息.由于这不在我的引导 jar 中,我想以某种方式添加到这个 database.properties 文件的路径,然后根据键读取条目.用于创建一个像这样的 bean:

I have created a standalone boot.jar that I need to start integrating into our higher environments. Each environment has a property file that contains database specific connection information. Since this does not live in my boot jar, I would like to somehow add the path to this database.properties file and then read the entries based on key. Used to create a bean like so:

<bean id="propertyLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
        <value>classpath:application.properties</value>
</property>

但是对于启动,我不知道该怎么做:但是我想指向下面的属性示例并提取我的值并以某种方式填充我在 dev 中硬编码的 application.properties 值.

but with boot, I am not sure how to do this: But I want to point to this below property example and pull out my values and somehow populate the application.properties values that I hardcoded in dev.

server:/config/database.properties
jdbc.username=TEST
jdbc.password=CHANGEME

更新我的 application.properites:

Updating my application.properites:

spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'

这样的事情我可以参数化我的 application.properties 文件.

Something like that do I can parameterize my application.properties file.

推荐答案

SpringBoot 提供配置文件,基本上允许您为每个环境拥有单独的 application.properties 文件.

SpringBoot offers profiles, which basically allows you to have separate application.properties file for each environment.

你可以有这样的东西:

public interface DataSourceConfig {}

@Component
@Profile("dev")
public DevDataSourceConfig implements DataSourceConfig{}

@Component
@Profile("prod")
public ProdDataSourceConfig implements DataSourceConfig{}

如果您将 spring 配置文件 "dev" 设置为活动状态,则只会实例化 DevDataSourceConfig bean,并在 Spring Environment 中实例化属性将被注入,将从 application-dev.properties 文件中读取.

If you have the spring profile "dev" set as active, only the DevDataSourceConfig bean will be instantiated and in Spring Environment the properties that will be injected, will be read from the application-dev.properties file.

类似地,当您激活 "prod" 配置文件时,只会实例化 ProdDataSourceConfig 并且将从 application-prod.properties<加载属性/code> 文件.

Similarly when you have the "prod" profile activated, only the ProdDataSourceConfig will be instantiated and the properties will be loaded from application-prod.properties file.

这允许您:

---
application-dev.properties
spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'
---
application-prod.properties
spring.datasource.username='PROD_jdbc.username'
spring.datasource.password='PROD_jdbc.password'

如果您想从文件系统上的自定义位置加载配置 - 您可以检查如何使用命令行参数传递该位置(docs)

If you want to load the configuration from a custom location on the file system - you can check how to pass the location with command line arguments (docs)

示例:

java -jar boot.jar --spring.config.location=classpath:/database.properties

这篇关于Spring Boot 读取基于类路径 arg 的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆