spring-cloud / spring-cloud/spring-cloud-stream

[Feature Request] Support loading customzied configuration for kafka binder configuration to kafka binder's context

Open
#2,446 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
1.1k
Forks
646
Avg merge
2d 3h
Merged PRs (30d)
8

Description

Describe the issue
This issue is to track the question in stack overflow.

I am having a request which needs to implement a BeanPostProcessor for KafkaBinderConfigurationProperties. While if I define this BeanPostProcessor as a static bean in my configuration class, I can see my BPP being loaded only to the application's application context instead of the kafka binder's context, so the BPP cannot take effects. Here my application uses only a single kafka binder.

The way I currently find to make it loaded to the binder's context is to add my configuration class for my BPP as the binder's sources through the property of spring.main.sources. But I am wondering is there any other solutions to make my BPP (or my configuration class for BPP) loaded to the binder's context without setting the configuration sources for the single binder case?

I read parts of the DefaultBinderFactory class, and find that it loads the outer application context as the parent of the binder's context, but for the BeanFactory, it cannot be inherited to the child during the context refreshment.

Here are the links for the source code of my BPP and Configuration. Due to the above reason I choose to make my configuration not a auto-configuration so it currently cannot be loaded into the normal application context as well.

To Reproduce
Steps to reproduce the behavior:

  1. Create a kafka binder project, e.g., this one.

  2. Create a custom BPP class like below:

    public class CustomKafkaBinderConfigurationPropertiesBeanPostProcessor implements BeanPostProcessor {
    	@Override
    	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    		if (bean instanceof KafkaBinderConfigurationProperties) {
    			KafkaBinderConfigurationProperties binderConfigurationProperties = (KafkaBinderConfigurationProperties) bean;
    			binderConfigurationProperties.getConfiguration().put("test", "test");
    		}
    		return bean;
    	}
    }
    
  3. Create a Configuration for the BPP like below and make sure it can be scanned/loaded to the application context.

    @Configuration
    public class CustomBeanPostProcessorConfiguration {
     
    	@Bean
    	static CustomKafkaBinderConfigurationPropertiesBeanPostProcessor customBeanPostProcessor() {
    		return new CustomKafkaBinderConfigurationPropertiesBeanPostProcessor();
    	}
    }
    
  4. The configuration cannot be loaded to the binder's context and thus the BPP cannot take effects unless similar properties being configured.

    spring.cloud.stream.binders.<binder-name>.environment.spring.main.sources
    

Version of the framework: org.springframework.cloud:spring-cloud-starter-stream-kafka:3.2.4
Expected behavior: the configuration can be loaded to the binder's context in the single binder case.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultBinderFactory and the KafkaBinderConfigurationProperties bean involved in the reported BeanPostProcessor behavior. Reproduce the issue with the uppercase-transformer sample and spring.cloud.stream.binders..environment.spring.main.sources; done means a documented, supported way for the configuration to load into the binder context for a single binder.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.