如何在PropertySourcesPlaceholderConfigurer春季启动中设置自动位置?

问题描述 投票:0回答:1

所以我在春季启动时开发了一些应用程序,我很困惑如何自动使用PropertySourcesPlaceholderConfigurer setlocation,这是我的代码

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;
}

有没有一种方法可以通过检测环境或类似的东西来自动设置位置?请帮助我,随时发表评论谢谢大家。

java spring spring-boot javabeans
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中以及application.properties中本地属性文件(src/main/resources/conf.properties)的内容。

  2. 在部署到Dev环境之前,请确保将dev配置文件设置为spring active配置文件,方法是将其添加到VM Options:-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

© www.soinside.com 2019 - 2024. All rights reserved.