簡體   English   中英

如何在 PropertySourcesPlaceholderConfigurer spring 引導中設置自動定位?

[英]How to set auto location in PropertySourcesPlaceholderConfigurer spring boot?

所以我在 spring 啟動中開發了一些應用程序,我很困惑如何自動使用 PropertySourcesPlaceholderConfigurer 設置位置,這是我的代碼

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

有沒有一種方法可以通過檢測環境或類似的方式自動設置位置? 請幫助我,隨時發表評論謝謝大家。

使用 Spring 引導的配置文件功能。

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

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

Spring 引導將自動加載 application.properties 的所有屬性以及您定義並設置為活動的配置文件特定屬性。

  1. 將開發屬性文件 ( /myth/app/data/weblogic_configuration/config/conf.properties ) 的內容保存在application-dev.properties中,並將本地屬性文件的內容 ( src/main/resources/conf.properties ) 保存在里面應用程序.properties。

  2. 在部署到開發環境之前,請確保將開發配置文件設置為 spring 活動配置文件,方法是將其添加到 VM 選項中: -Dspring.profiles.active=dev

    這將加載application-dev.properties中的屬性。

    灣。 在本地運行您的應用程序時,刪除或不指定活動配置文件。

請參閱以下鏈接以供參考: https://docs.spring.io/spring-boot/docs/1.1.x/reference/html/boot-features-profiles.ZFC35FDC70D5FC69D2539883A822EZ7C

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM