我是 spring 的新手,想知道如何将基于 java 的配置转换为基于 xml 的 bean 配置。我知道现在更多地使用基于注释的配置。但我的要求是使用基于 xml 的配置。 下面添加了Bean配置。
@Bean
DataStoreWriter<String> dataStoreWriter(org.apache.hadoop.conf.Configuration hadoopConfiguration) {
TextFileWriter writer = new TextFileWriter(hadoopConfiguration, new Path(basePath), null);
return writer;
最佳答案
可以直接在xml配置中创建bean
<bean id="dataStoreWriter" class="TextFileWriter">
<constructor-arg index="0" ref="hadoopConfigBean"/>
<constructor-arg index="1">
<bean class="Path">
<constructor-arg index="0" value="/tmp"/>
</bean>
</constructor-arg>
</bean>
如果您需要重要的 bean 配置,那么您可以在 xml 配置中使用工厂方法调用
<bean id="dataStoreWriter" class="DataStoreFactory" factory-method="dataStoreWriter">
<constructor-arg index="0" ref="hadoopConfigBean"/>
<constructor-arg index="1" value="/tmp"/>
</bean>
工厂类应该是这样的
public class DataStoreFactory {
public static DataStoreWriter<String> dataStoreWriter(Configuration hadoopConfiguration, String basePath) {
// do something here
TextFileWriter writer = new TextFileWriter(hadoopConfiguration, new Path(basePath), null);
return writer;
}
}
关于java - 如何将基于java的bean配置转换为基于xml的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36746020/