grpc-ecosystem / grpc-spring

Spring Boot starter module for gRPC framework.
https://grpc-ecosystem.github.io/grpc-spring/
Apache License 2.0
3.48k stars 812 forks source link

GrpcGlobalServerInterceptor override in case of another bean of some type. #951

Open 313hemant313 opened 1 year ago

313hemant313 commented 1 year ago

Wanted to override a base interceptor with another one, had tried adding ConditionalOnMissingBean but its not working and both the interceptors are registered.

@GrpcGlobalServerInterceptor
@Order(1100)
**@ConditionalOnMissingBean**(GrpcResponseServerInterceptor.class)
class BaseInterceptor extends ServerInterceptor # This is common interceptor (Usually in other modules this is fine)

@GrpcGlobalServerInterceptor
@Order(1100)
class CustomServiceInterceptor extends BaseInterceptor implements GrpcResponseServerInterceptor

Also tried Bean aware approach but got error while adding to HashBiMap (value already present)

beanFactoryAwareOrderComparator(final ApplicationContext context,
            final Class<?> beanType) {
final Map<?, String> beans = HashBiMap.create(context.getBeansOfType(beanType)).inverse();
    @GrpcGlobalServerInterceptor
    @Order(999)
    ServerInterceptor serverInterceptor() {
        if (null != applicationContext.getBean(GrpcResponseServerInterceptor.class)) {
            return applicationContext.getBean(GrpcResponseServerInterceptor.class);
        }
        return new BaseInterceptor();
    }