How to set auto location in PropertySourcesPlaceholderConfigurer spring boot?

huangapple 未分类评论51阅读模式
英文:

How to set auto location in PropertySourcesPlaceholderConfigurer spring boot?

问题

以下是翻译好的部分:

package com.org.tre.myth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;

@Configuration
public class ExternalPropertyConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
        properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
        properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
        properties.setIgnoreResourceNotFound(false);
        return properties;
    }
}

当我在本地环境时我需要通过注释来停用开发位置

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    //properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
    properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

在将应用部署到开发环境之前我需要通过注释本地位置并激活开发位置

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
    //properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

是否有一种方式可以自动根据环境设置位置或者类似的方法请帮帮我随意发表评论谢谢
英文:

So i was develop some app in spring boot, i'm so confused how to used PropertySourcesPlaceholderConfigurer setlocation automatically, here is my code

package com.org.tre.myth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;

@Configuration
public class ExternalPropertyConfig {

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
    properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

}

When i'm on local i need to deactivate dev location using comment

 @Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
   //properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
    properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

And before i deploy my app in dev, i need to do opposite thing by comment my local location and activate dev location

 @Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties")); //devpconfprop
    //properties.setLocation(new FileSystemResource("src/main/resources/conf.properties")); //localconfprop
    properties.setIgnoreResourceNotFound(false);
    return properties;
}

is there is a way it can do automatically set the location by detecting environment or some kind like that? please help me, feel free to comment
Thanks guys.

答案1

得分: 0

使用Spring Boot的配置文件特性。

您可以按照以下步骤操作:

  1. 创建环境特定的配置文件:
  • application.properties
  • application-dev.properties

Spring Boot将自动加载application.properties中的所有属性以及您定义并设置为活动的特定配置文件属性。

  1. 将开发环境属性文件 (/myth/app/data/weblogic_configuration/config/conf.properties) 的内容保存到 application-dev.properties 中,
    并将本地属性文件 (src/main/resources/conf.properties) 的内容保存到 application.properties 中。

  2. 在部署到开发环境之前,确保通过将以下内容添加到VM选项中,将开发环境配置设置为Spring的活动配置:
    -Dspring.profiles.active=dev

    这将加载 application-dev.properties 中的属性,并覆盖 application.properties 中的属性。

    b. 在本地运行应用程序时,删除或不指定活动配置。

参考链接:
https://docs.spring.io/spring-boot/docs/1.1.x/reference/html/boot-features-profiles.html

英文:

Use Spring Boot's Profiles feature.

You can follow the steps below:

  1. Create environment specific profiles:
  • application.properties
  • application-dev.properties

Spring Boot will automatically load all the properties of the application.properties and the profile specific properties you defined and set as active.

  1. Save the content of your dev properties file (/myth/app/data/weblogic_configuration/config/conf.properties) inside application-dev.properties
    and the content of your local properties file (src/main/resources/conf.properties) inside application.properties.

  2. Before deploying to Dev environment, make sure to set the dev profile as the spring active profile by adding this to the VM Options:
    -Dspring.profiles.active=dev

    This will load the properties inside your application-dev.properties and overrides the properties inside your application.properties.

    b. When running your your app locally, remove or do not specify active profile.

See link below for reference:
https://docs.spring.io/spring-boot/docs/1.1.x/reference/html/boot-features-profiles.html

huangapple
  • 本文由 发表于 2020年5月4日 12:45:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61585344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定