2016-12-27 50 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; 
     } 
    }