私はSpringブートとSpring統合を使用しており、それぞれchild()
コンテキストに対して異なるプロパティをロードしたいと考えています。これは可能ですか?このときSpringブート動的コンテキスト作成(親/子)
、私はこれで働いています:(唯一の最も関連性の高いライン)
SpringApplicationBuilder parent = new SpringApplicationBuilder(Main.class);
// Child sources (Will be inside a loop)
// Load the different environment ??
parent.child(ConfigDynamic.class);
// End of child loading
parent.run(args);
私はSpringApplicationBuilder child()
方法を検討しているとプロパティが子供に父親から伝播されています
child.properties(this.defaultProperties).environment(this.environment)
.additionalProfiles(this.additionalProfiles);
しかし、私は、次の例のように、動的にいくつかのプロパティをロードする必要があります。
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(Main.class);
parent.setId("parent");
Properties props = new Properties();
StandardEnvironment env = new StandardEnvironment();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.setId("child" + ++n);
child.setParent(parent);
child.register(ConfigDynamic.class);
// populate properties for this adapter
props.setProperty("prop1", myProp1);
props.setProperty("prop2", myProp2);
env.getPropertySources().addLast(pps);
child.setEnvironment(env);
child.refresh();
これは、いくつかのSpring Integrationコンポーネントが構成ファイルから動的にロードされるためです。したがって、私は動的なコンポーネントと異なるコンテキストを作成する必要があります。
任意のアイデアが理解されるであろう、ありがとう
編集1:私は例更新
:
SpringApplicationBuilder parent = new SpringApplicationBuilder(Main.class);
for start
SpringApplicationBuilder child = parent.child(DynamicConfig.class);
//Properties creation..
child.context().getEnvironment().getPropertySources().addLast(pps);
child.run(args);
end for
parent.run(args);
しかし、今、child()
コンテキストはrun()
方法や昇給の前にヌルですNPE。
編集2:私はすべての私の@Component
と@Configuration
は、春の統合コンポーネントであり、ロードすることができたとしても親コンテキストを必要とする理由(ワーキング)は
SpringApplicationBuilder parent = new SpringApplicationBuilder(Main.class);
for start
SpringApplicationBuilder child = parent.child(DynamicConfig.class);
PropertiesPropertySource pps = new PropertiesPropertySource("dynamicProps", props);
StandardEnvironment env = new StandardEnvironment();
env.getPropertySources().addLast(pps);
child.environment(env);
child.run();
end for
parent.run(args);
私は間違いなく理解していない最後の事はありますchild()
となります。親コンテクストは、バス/コンポーネントを入れて別の子どもと通信する場所ですか?私はそれぞれの文脈のためにいくつかのシングルトンを読み込むメモリの問題かもしれません。
子()、nullポインタ例外を発生させます。私は質問を更新しました。 – crm86