Spring FactoryBean作为PropertySourcesPlaceholderConfigurer的参考

时间:2016-12-27 10:10:26

标签: spring spring-java-config

创建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的等价物是什么?我没有找到任何相同的参考。

1 个答案:

答案 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;
        }
    }
相关问题