简体   繁体   English

如何在 Spring 启动 Maven 项目中移动 application.properties 或执行外部属性

[英]How to Move application.properties or Do External Properties in Spring Boot Maven Project

So i was develop some application in Spring Boot, but i get stumbled upon application.properties file, i want to make it easier to use, cause i have some environment to test, like Dev, Uat and Production所以我在 Spring Boot 中开发了一些应用程序,但是我偶然发现了 application.properties 文件,我想让它更容易使用,因为我有一些环境要测试,比如 Dev、Uat 和 Production

When i want to launch the app, i build it using mvn clean install with application.properties profile on the environment.当我想启动应用程序时,我使用 mvn clean install 和环境中的 application.properties 配置文件来构建它。 cause my app only using war snapshot for backend service.导致我的应用程序仅将战争快照用于后端服务。

like when i want to launch in dev就像我想在开发中启动一样

spring.profiles.active=dev

or when i want to deploy it in uat environment i'm using或者当我想在我正在使用的 uat 环境中部署它时

spring.profiles.active=uat

is there a way to make it easier, like using external prop, so i don't need using application.properties anymore, cause i'll upload application.properties in each of my environment, so the app can read from external properties.有没有办法让它变得更容易,比如使用外部道具,所以我不再需要使用 application.properties,因为我会在我的每个环境中上传 application.properties,所以应用程序可以从外部属性中读取。

i already tried PropertySourcePlaceholderConfigurer, but no luck我已经尝试过 PropertySourcePlaceholderConfigurer,但没有运气

@Configuration
public class ExternalPropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    final PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();

    Resource[] resources = new Resource[ ] {

    new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_uat_configuration.properties"),
    new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_dev_configuration.properties"),
    new FileSystemResource("/src/main/resources/myth_local_configuration.properties")
    };

    properties.setLocations( resources );
    properties.setIgnoreResourceNotFound(true);
    properties.setIgnoreUnresolvablePlaceholders(true);
    return properties;
    }
}

After building the jar you can launch it like this构建 jar 后,您可以像这样启动它

java -jar -Dspring.profiles.active=dev "app.jar"

and providing the active profile will make that particular profile take effect并提供活动配置文件将使该特定配置文件生效

You just create the profile scoped properties file like application-dev.properties or application-prod.properties您只需创建配置文件范围的属性文件,如application-dev.propertiesapplication-prod.properties

They are automatically loaded.它们会自动加载。 No need for code to load them无需代码加载它们

You can have a look at this Spring Boot Features你可以看看这个Spring Boot Features

you can load properties using xml configuration as below:您可以使用 xml 配置加载属性,如下所示:

<bean id="configProperties"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesMode" value="2" />
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
                <value>classpath:nosql.properties</value>
                <value>classpath:api.properties</value>
            </list>
        </property>
</bean>

After this you can use property spring.profiles.active defined in any of your properties file (any of db.properties , nosql.properties or api.properties ) in your code ->在此之后,您可以使用在您的任何属性文件中定义的属性spring.profiles.active (任何db.propertiesnosql.propertiesapi.properties )在您的属性代码中

@Service("bean")
public class Bean {
    @Value("${spring.profiles.active}")
    private String profile;
}

I think this is what you are looking for Spring boot YML doc .我认为这就是您正在寻找的Spring boot YML doc Generally externalizing the config is achieved through ymls.通常外部化配置是通过 ymls 实现的。

暂无
暂无

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

相关问题 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? Spring Boot 外部应用程序.properties - Spring boot external application.properties 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 如何在 Maven pom.xml 中重用 application.properties (Spring Boot) 中的属性? - How to reuse properties from application.properties (Spring Boot) in Maven pom.xml? 如何使用Spring Boot在application.properties中注入Maven配置文件的属性 - how to inject maven profile's properties in application.properties with spring boot Spring Boot应用程序不拉入外部application.properties文件 - Spring boot application not pulling external application.properties file in 在application.properties中具有随机值的Spring Boot Maven构建问题 - Spring Boot Maven Build Problem with Random Values in application.properties Spring 引导类路径配置文件覆盖外部 application.properties - Spring boot classpath configuration files overrides external application.properties 在jar spring boot外部目录中搜索application.properties - Search application.properties in directory external to jar spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM