2016-12-27 54 views
1

创建FactoryBeany以从Zookeeper加载属性。Spring FactoryBean作为PropertySourcesPlaceholderConfigurer的参考

public class ZkPropertiesFactoryBean extends AbstractFactoryBean<Properties> {..} 

在使用XML配置,可以如下

<bean id="zkProperties" class="test.ZkPropertiesFactoryBean"/> 
<context:property-placeholder properties-ref="zkProperties"/> 

我想XML配置转换成Java的配置,使用PropertySourcesPlaceholderConfigurer

@Bean 
    public static PropertySourcesPlaceholderConfigurer loadProperties() throws Exception { 
     PropertySourcesPlaceholderConfigurer prop = new PropertySourcesPlaceholderConfigurer(); 
     prop.setIgnoreUnresolvablePlaceholders(true); 
     return prop; 
    } 

什么是指豆相当于properties-ref?我无法找到任何参考。

回答

1

您可以使用PropertySourcesPlaceholderConfigurersetProperties和使用您的AbstractFactoryBean.getObject()方法对其进行设置:

你的配置可能是这个样子:

@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; 
     } 
    }