spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
993 stars 605 forks source link

Override StreamListenerAnnotationBeanPostProcessor not work #990

Closed wiselyman closed 7 years ago

wiselyman commented 7 years ago

I overrided StreamListenerAnnotationBeanPostProcessor like:

@Bean(name = STREAM_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_NAME)
    public BeanPostProcessor streamListenerAnnotationBeanPostProcessor() {

    }

The configuration only woks when put it in the main class, and not work when put it in other @Configuration class.

Thanks for your help

mbogoevici commented 7 years ago

@wiselyman The methor registering the BeanPostProcessor should be declared static - see the comment here: http://forum.spring.io/forum/spring-projects/container/123899-beanpostprocessor-with-bean-annotation-not-working .

wiselyman commented 7 years ago

@mbogoevici I have done like this already, and not work too. with static or no static works in main class,not work in other configuration classes

mbogoevici commented 7 years ago

@wiselyman That's strange. Would you mind sharing a sample that doesn't work?

wiselyman commented 7 years ago

@mbogoevici https://github.com/wiselyman/Spring-Cloud-Stream-Kafka-CQRS-ES/blob/master/statistics/src/main/java/com/example/StatisticsApplication.java.

I want to learn from https://dturanski.wordpress.com/2017/03/26/spring-cloud-stream-for-event-driven-architectures/ and do like this:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EventHandlerConfig.class})
public @interface EnableEventHandling {

}

and put StreamListenerAnnotationBeanPostProcessorin EventHandlerConfigclass,but with no luck.

mbogoevici commented 7 years ago

Hi,

Dug a bit into your example: the issue manifests itself when the configuration is in a package or subpackage of the @SpringBootApplication class - in that case, component scanning will cause the bean to register before the default framework bean (i.e. you can try placing them your configuration class and annotation in a separate package that is not a subpackage of com.example - that would be somewhat expected if you wanted to put this in a separate library and reuse this in other applications). As a fix on our end, we could make the framework-provided BeanPostProcessor @ConditionalOnMissingBean.

wiselyman commented 7 years ago

@mbogoevici thanks ,it works.