Example usage for org.springframework.beans.factory.support BeanDefinitionBuilder genericBeanDefinition

List of usage examples for org.springframework.beans.factory.support BeanDefinitionBuilder genericBeanDefinition

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support BeanDefinitionBuilder genericBeanDefinition.

Prototype

public static <T> BeanDefinitionBuilder genericBeanDefinition(Class<T> beanClass,
        Supplier<T> instanceSupplier) 

Source Link

Document

Create a new BeanDefinitionBuilder used to construct a GenericBeanDefinition .

Usage

From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java

@SuppressWarnings({ "unchecked" })
private void buildStreamsBuilderAndRetrieveConfig(Method method, ApplicationContext applicationContext,
        String inboundName) {//from  w ww  . ja v a  2 s . com
    ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory();

    Map<String, Object> streamConfigGlobalProperties = applicationContext
            .getBean("streamConfigGlobalProperties", Map.class);

    KafkaStreamsConsumerProperties extendedConsumerProperties = this.kafkaStreamsExtendedBindingProperties
            .getExtendedConsumerProperties(inboundName);
    streamConfigGlobalProperties.putAll(extendedConsumerProperties.getConfiguration());

    String applicationId = extendedConsumerProperties.getApplicationId();
    //override application.id if set at the individual binding level.
    if (StringUtils.hasText(applicationId)) {
        streamConfigGlobalProperties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    }

    int concurrency = this.bindingServiceProperties.getConsumerProperties(inboundName).getConcurrency();
    // override concurrency if set at the individual binding level.
    if (concurrency > 1) {
        streamConfigGlobalProperties.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, concurrency);
    }

    Map<String, KafkaStreamsDlqDispatch> kafkaStreamsDlqDispatchers = applicationContext
            .getBean("kafkaStreamsDlqDispatchers", Map.class);

    KafkaStreamsConfiguration kafkaStreamsConfiguration = new KafkaStreamsConfiguration(
            streamConfigGlobalProperties) {
        @Override
        public Properties asProperties() {
            Properties properties = super.asProperties();
            properties.put(SendToDlqAndContinue.KAFKA_STREAMS_DLQ_DISPATCHERS, kafkaStreamsDlqDispatchers);
            return properties;
        }
    };

    StreamsBuilderFactoryBean streamsBuilder = this.cleanupConfig == null
            ? new StreamsBuilderFactoryBean(kafkaStreamsConfiguration)
            : new StreamsBuilderFactoryBean(kafkaStreamsConfiguration, this.cleanupConfig);
    streamsBuilder.setAutoStartup(false);
    BeanDefinition streamsBuilderBeanDefinition = BeanDefinitionBuilder
            .genericBeanDefinition((Class<StreamsBuilderFactoryBean>) streamsBuilder.getClass(),
                    () -> streamsBuilder)
            .getRawBeanDefinition();
    ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("stream-builder-" + method.getName(),
            streamsBuilderBeanDefinition);
    StreamsBuilderFactoryBean streamsBuilderX = applicationContext
            .getBean("&stream-builder-" + method.getName(), StreamsBuilderFactoryBean.class);
    this.methodStreamsBuilderFactoryBeanMap.put(method, streamsBuilderX);
}