application.ymlからプロパティを読み込むときに春のプロファイルは無視されます

ヘクター:

私は、Springコンテキストをスキャンし、このコードを持っています:

public void scan() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    context.register(SomeConfig.class);
    context.refresh();
}

私は、プロパティから読み取る必要がapplication.ymlそうで、ファイルSomeConfig、クラス、私はこれを持っています:

@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertyLoaderFactory.class)
public class SomeConfig {
  //some beans
}

(私はからYamlPropertyLoaderFactoryクラスをコピーしたこちら

application.yml プロファイルによって、いくつかのプロパティを持つ典型的な春ブートファイル、およびデフォルトのプロファイルです:

spring:
  profiles:
    active: p1

---

spring:
   profiles: p1

file: file1.txt

---

spring:
   profiles: p2

file: file2.txt

いくつかの豆では、私が読んでいるfile使用してプロパティを@Value

私は自分のアプリケーションを実行すると、私は渡している-Dspring.profiles.active=p1変数を、私はエラーを取得しています:

「$ {ファイル}」値のプレースホルダ「ファイル」を解決できませんでした。

(これはapplication.ymlがP1にデフォルトのプロファイルを設定しているので、私はどのプロファイルを渡さない場合でも動作するはずです)

私はからconfigのすべてのプロファイルを削除するとapplication.yml、それが正常に動作します:

file: file1.txt

だから、それはコンテキストスキャンプロファイル変数を読んでいないことを意味します。

私は、「プログラム的に」アクティブなプロファイルを設定する場合にも、それはどちらかのプロパティを解決しません。

context.getEnvironment().setActiveProfiles("p1");
pcoates:

YamlPropertyLoaderFactoryあなたが参照するには、次のコードがあります。

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }

        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), null);
    }
}

第3のパラメータYamlPropertySourceLoader.load()の方法は、実際にあなたがプロパティをしたいというプロファイル名です。この例では、ヌルで通過すると、それだけではない、特定のプロファイルのYMLファイルからプロパティのセットを返します。

すなわち

spring:
  profiles:
    active: p1

---

私はそれがアクティブプロファイル名を拾うのは簡単だとは思わないYamlPropertyLoaderFactoryあなたのような何かを試みることができるものの、...

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }

        String activeProfile = System.getProperty("spring.profiles.active");
        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), activeProfile);
    }
}

あなたはYMLファイルでアクティブなプロファイル名を持っているとして、あるいは、あなたが呼び出すことができYamlPropertySourceLoader().load、その後spring.profiles.activeプロパティを取得したいYMLファイルの実際の一部をロードするために、再びそれを呼び出すためにnullを。

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }
        PropertySource<?> source = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), null);
        String activeProfile = source.getProperty("spring.profiles.active");
        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), activeProfile);
    }
}

YamlPropertySourceLoader2018年2月におけるバック(変更されたGitのレポでYamlPropertySourceLoaderの非難ビュー)。それは今propertySourceのリストを返し、loadメソッドの3番目のパラメータはありません。

あなたは新しいバージョンで次の手順を実行することができるだろうYMLファイルにspring.profiles.active性質を持って提供 YamlPropertySourceLoader

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }
        List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
        for (PropertySource<?> checkSource : sources) {
            if (checkSource.containsProperty("spring.profiles.active")) {
                String activeProfile = (String) checkSource.getProperty("spring.profiles.active");
                for (PropertySource<?> source : sources) {
                    if (activeProfile.trim().equals(source.getProperty("spring.profiles"))) {
                        return source; 
                    }
                }
            }
        }
        return sources.get(0);
    }

}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=181769&siteId=1