使用BeanDefinitionRegistryPostProcessor创建N个bean

时间:2018-11-24 22:26:37

标签: spring spring-boot

我正在尝试使用BeanDefinitionRegistryPostProcessor动态创建 N 个bean。基于this问题,我选择将BeanDefinitionRegistryPostProcessor用于我的用例。

我在application.yml中定义了以下内容:

app:
  downstream-services:
    rest:
      jsonPlaceHolder:
        url: https://jsonplaceholder.typicode.com/todos
        api-type: io.mateo.dynamicbeans.JsonPlaceHolderApi

在这里https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignConfigurationProperties.java

与哪个ConfigiruationProperties类建立连接

然后我想将该ConfigiruationProperties类与我在此处定义的工厂bean一起注入:https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientAutoConfiguration.java

所以现在我有以下内容:

https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientFactoryPostProcessor.java

@Component
public class FeignClientFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    private final FeignConfigurationProperties properties;
    private final FeignClientFactory feignClientFactory;

    public FeignClientFactoryPostProcessor(FeignConfigurationProperties properties, FeignClientFactory feignClientFactory) {
        this.properties = properties;
        this.feignClientFactory = feignClientFactory;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        properties.getDownstreamServices().getRest().forEach((beanName, props) -> makeClient(beanName, props, registry));
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // no-op
    }

    private void makeClient(String beanName, FeignClientProperties props, BeanDefinitionRegistry registry) {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(props.getApiType());
        beanDefinition.setInstanceSupplier(() -> feignClientFactory.create(props));
        registry.registerBeanDefinition(beanName, beanDefinition);
    }
}

它应该创建的单个bean是在这里插入服务类:https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/JsonPlaceHolderService.java

我遇到的问题是:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.mateo.dynamicbeans.FeignClientFactoryPostProcessor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    ... 18 common frames omitted

但是当我从两个属性和定义的构造函数中删除final关键字时,我得到一个NullPointerException

那么我如何动态创建 N 数量的bean,以便它们及时可用供我的任何@Service类使用?

>

我知道https://spring.io/projects/spring-cloud-openfeign。我在这里重新创建了我的问题,以说明在另一个项目中通过动态创建SOAP客户端遇到的相同问题。

更新:进行以下更改:https://github.com/ciscoo/dynamicbeans/commit/4f16de9d03271025cd65d95932a3e854c0619c29,现在我可以完成用例。

1 个答案:

答案 0 :(得分:2)

正如您所链接的问题的答案所建议的那样,您不能将依赖项注入到bean工厂的后处理器中。您需要以编程方式绑定它,而不是注入配置属性类。在Spring Boot 2.x中,即achieved using the Binder API

  

新的Binder API也可以在@ConfigurationProperties之外直接在您自己的代码中使用。例如,以下内容将绑定到List个对象中的一个PersonName

List<PersonName> people = Binder.get(environment)
    .bind("my.property", Bindable.listOf(PersonName.class))   
    .orElseThrow(IllegalStateException::new);
     

配置源可以这样用YAML表示:

my:
  property:
  - first-name: Jane
    last-name: Doe
  - first-name: John
    last-name: Doe