创建FactoryBeany以从Zookeeper加载属性。
public class ZkPropertiesFactoryBean extends AbstractFactoryBean<Properties> {..}
使用XML配置时,可以按如下方式引用bean
<bean id="zkProperties" class="test.ZkPropertiesFactoryBean"/>
<context:property-placeholder properties-ref="zkProperties"/>
我尝试使用PropertySourcesPlaceholderConfigurer将XML配置转换为Java配置
@Bean
public static PropertySourcesPlaceholderConfigurer loadProperties() throws Exception {
PropertySourcesPlaceholderConfigurer prop = new PropertySourcesPlaceholderConfigurer();
prop.setIgnoreUnresolvablePlaceholders(true);
return prop;
}
properties-ref
的等价物是什么?我没有找到任何相同的参考。
答案 0 :(得分:1)
您可以使用PropertySourcesPlaceholderConfigurer
的{{3}}并使用setProperties
方法进行设置:
您的配置可能如下所示:
@Configuration
public class ZookeeperConfig {
@Autowired
private ZkPropertiesFactoryBean zkPropertiesFactoryBean;
@Bean
public static PropertySourcesPlaceholderConfigurer loadProperties() throws Exception {
PropertySourcesPlaceholderConfigurer prop = new PropertySourcesPlaceholderConfigurer();
prop.setIgnoreUnresolvablePlaceholders(true);
prop.setProperties(zkPropertiesFactoryBean.getObject());
return prop;
}
}