从属性文件创建 Spring Boot 动态 Bean

Geo*_*rge 3 spring spring-boot

如何根据application.yml文件动态定义bean?

例如,YAML 文件如下所示:

service:
   host: http://localhost:8080/
   account:
     url: /account
     content-type: application/json
   registry:
     url: /registry
     content-type: application/xml
Run Code Online (Sandbox Code Playgroud)

这将动态创建两个HttpHeaders带有Content-Type标头集的。

下面是我现在定义 bean 的方式:

@Bean
public HttpHeaders accountHeaders(
    @Value("${service.account.content-type}") String contentType
) {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_TYPE, contentType);
    return headers;
}

@Bean
public HttpHeaders registryHeaders(
    @Value("${service.registry.content-type}") String contentType
) {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_TYPE, contentType);
    return headers;
}
Run Code Online (Sandbox Code Playgroud)

如果我需要添加更多端点,我需要复制和粘贴这些 bean,我想避免这种情况。

注意:这些动态 bean 不需要任何其他 bean。我不确定这是否有所作为。它只需要加载配置。

Ali*_*nik 9

您可以如下所述注入所有属性(不确定如何使用当前的属性结构来执行此操作,spring 允许有关属性注入的真正高级功能,此处有更多示例)

@ConfigurationProperties(prefix = "yourPrefix")
public class CustomProperties {

  private final Map<String, String> properties = new HashMap<>();

  @Autowired 
  private ApplicationContext applicationContext;      

  @PostConstruct
  public void init() {
    AutowireCapableBeanFactory beanFactory = this.applicationContext.getAutowireCapableBeanFactory();
    // iterate over properties and register new beans
  }

}
Run Code Online (Sandbox Code Playgroud)

您可以使用类似的方法手动注册bean

beanFactory.registerSingleton("beanName", bean);
Run Code Online (Sandbox Code Playgroud)

动态 Bean 注册的其他示例在这里


小智 8

有几个选项:

  • 使用程序化(“功能性”)bean 注册。通过这种方式,注册 bean 是一个函数,您可以使用 for 循环和 if/else 等。 Aliaksei 的示例演示了这一点,有点。我通常使用ApplicationContextInitializer带有SpringApplicationBuilder()(而不是SpringApplication.run(..))的注册。
  • 你可以使用一个ImportBeanDefinitionRegistrar. 实现它,然后使用BeanDefinitions. 使用@Import(MyIbdr.class).
package com.example.dynabeans;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.util.UUID;

@SpringBootApplication
public class DynabeansApplication {

    public static void main(String[] args) {
        SpringApplication.run(DynabeansApplication.class, args);
    }

}

class Foo {

    private final String id = UUID.randomUUID().toString();

    @Override
    public String toString() {
        return "Foo{" + id + "}";
    }
}


@Component
class FooListener {

    private final Log log = LogFactory.getLog(getClass());

    FooListener(Foo[] foos) {
        log.info("there are " + foos.length + " " + Foo.class.getName() + " instances.");
    }

}

@Component
class LoopyBeanRegistrar implements BeanDefinitionRegistryPostProcessor {

    private final Log log = LogFactory.getLog(getClass());
    private final int max = (int) (Math.random() * 100);

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        log.info("registering " + max + " beans.");
        for (int i = 0; i < max; i++) {
            BeanDefinitionBuilder gdb = BeanDefinitionBuilder.genericBeanDefinition(Foo.class, () -> new Foo());
            AbstractBeanDefinition abd = gdb.getBeanDefinition();
            BeanDefinitionHolder holder = new BeanDefinitionHolder(abd, Foo.class.getName() + '#' + i, new String[0]);
            BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

使用环境对其进行测试,看起来它工作正常。不过,您必须在配置中外部化您的注册商才能注入环境。这里不是Binder强制性的。env.getProperty()也会以同样的方式工作。

@Configuration
public class DynamicBeansConfiguration {

    @Bean
    public BeanDefinitionRegistrar beanDefinitionRegistrar(Environment environment) {
        return new BeanDefinitionRegistrar(environment);
    }

    public class BeanDefinitionRegistrar implements BeanDefinitionRegistryPostProcessor {
        private Environment environment;

        public BeanDefinitionRegistrar(Environment environment) {
            this.environment = environment;
        }

        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

            List<Developer> developers = Binder.get(environment)
                    .bind("developers", Bindable.listOf(Developer.class))
                    .orElseThrow(IllegalStateException::new);

            developers.forEach(developer -> {
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(Developer.class);
                beanDefinition.setInstanceSupplier(() -> new Developer(developer.getName()));
                registry.registerBeanDefinition(developer.getName(), beanDefinition);
            });
        }

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

developers=John,Jack,William
Run Code Online (Sandbox Code Playgroud)