Spring FactoryBean 作为 PropertySourcesPlaceholderConfigurer 的参考

Spring FactoryBean as reference for PropertySourcesPlaceholderConfigurer

正在创建 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 等价于什么?我找不到任何参考资料来做同样的事情。

您可以使用setProperties of the PropertySourcesPlaceholderConfigurer and set it by using your 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;
        }
    }