PropertyPlaceholderConfigurersはコンテナーごとに機能すると思います。そのため、xmlインポートではこれを実現できません。
再The application would have two beans named a and b now available with the same definition but as distinct instances
現在のアプリケーションコンテキストを親アプリケーションコンテキストとして使用して、追加のアプリケーションコンテキスト(たとえば、ClassPathXmlApplicationContext)を手動で作成することを検討する必要があると思います。したがって、many objects created with interdependencies
セットはそれぞれ独自のコンテナに常駐します。
b
ただし、この場合、 -containerから-beansを参照することはできませんa
。
更新BeanDefinitionRegistryPostProcessor専用Beanを登録することで、Bean定義を手動で後処理(新しい定義を追加)できます が、この解決策も簡単ではないようです。
OK、これがxmlファイルを手動でインポートする私の大まかな試みです:
免責事項:私は実際には非常に悪いJava ioプログラマーなので、リソース関連のコードを再確認してください:-)
public class CustomXmlImporter implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
private Map<String, String> properties;
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public Map<String, String> getProperties() {
return properties;
}
private void readXml(XmlBeanDefinitionReader reader) {
InputStream inputStream;
try {
inputStream = new ClassPathResource(this.classpathXmlLocation).getInputStream();
} catch (IOException e1) {
throw new AssertionError();
}
try {
Scanner sc = new Scanner(inputStream);
try {
sc.useDelimiter("\\A");
if (!sc.hasNext())
throw new AssertionError();
String entireXml = sc.next();
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${",
"}", null, false);
Properties props = new Properties();
props.putAll(this.properties);
String newXml = helper.replacePlaceholders(entireXml, props);
reader.loadBeanDefinitions(new ByteArrayResource(newXml.getBytes()));
} finally {
sc.close();
}
} finally {
try {
inputStream.close();
} catch (IOException e) {
throw new AssertionError();
}
}
}
private String classpathXmlLocation;
public void setClassPathXmlLocation(String classpathXmlLocation) {
this.classpathXmlLocation = classpathXmlLocation;
}
public String getClassPathXmlLocation() {
return this.classpathXmlLocation;
}
@Override
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
readXml(reader);
}
}
XML構成:
<bean class="CustomXmlImporter">
<property name="classPathXmlLocation" value="a.xml" />
<property name="properties">
<map>
<entry key="key" value="a" />
</map>
</property>
</bean>
<bean class="CustomXmlImporter">
<property name="classPathXmlLocation" value="a.xml" />
<property name="properties">
<map>
<entry key="key" value="b" />
</map>
</property>
</bean>
このコードは、クラスパスからリソースをロードします。そのようなことをする前に、私はよく考えます、とにかく、あなたはこれを出発点として使うことができます。