55import org .lognet .springboot .grpc .autoconfigure .GRpcServerProperties ;
66import org .springframework .beans .factory .BeanCreationException ;
77import org .springframework .beans .factory .DisposableBean ;
8- import org .springframework .beans .factory .InitializingBean ;
98import org .springframework .beans .factory .annotation .Autowired ;
109import org .springframework .beans .factory .config .BeanDefinition ;
1110import org .springframework .boot .CommandLineRunner ;
12- import org .springframework .context .ApplicationContext ;
1311import org .springframework .context .support .AbstractApplicationContext ;
1412import org .springframework .core .type .StandardMethodMetadata ;
15- import org .springframework .util .StreamUtils ;
1613
1714import java .lang .annotation .Annotation ;
1815import java .util .Collection ;
1916import java .util .List ;
20- import java .util .Map ;
2117import java .util .Optional ;
22- import java .util .function .Function ;
2318import java .util .stream .Collectors ;
2419import java .util .stream .Stream ;
2520
2924@ Slf4j
3025public class GRpcServerRunner implements CommandLineRunner ,DisposableBean {
3126
27+ private class NamedBeanWrapper <T >{
28+ private T bean ;
29+ private String beanName ;
30+
31+ public NamedBeanWrapper ( String beanName ,T bean ) {
32+ this .bean = bean ;
33+ this .beanName = beanName ;
34+ }
35+
36+ public T getBean () {
37+ return bean ;
38+ }
39+
40+ public void setBean (T bean ) {
41+ this .bean = bean ;
42+ }
43+
44+ public String getBeanName () {
45+ return beanName ;
46+ }
47+
48+ public void setBeanName (String beanName ) {
49+ this .beanName = beanName ;
50+ }
51+ }
52+
3253 @ Autowired
3354 private AbstractApplicationContext applicationContext ;
3455
@@ -41,14 +62,17 @@ public class GRpcServerRunner implements CommandLineRunner,DisposableBean {
4162 public void run (String ... args ) throws Exception {
4263 log .info ("Starting gRPC Server ..." );
4364
44- Collection <ServerInterceptor > globalInterceptors = getTypedBeansWithAnnotation (GRpcGlobalInterceptor .class ,ServerInterceptor .class );
65+ Collection <ServerInterceptor > globalInterceptors = getTypedBeansWithAnnotation (GRpcGlobalInterceptor .class ,ServerInterceptor .class )
66+ .stream ()
67+ .map (NamedBeanWrapper ::getBean )
68+ .collect (Collectors .toList ());
4569 final ServerBuilder <?> serverBuilder = ServerBuilder .forPort (gRpcServerProperties .getPort ());
4670
4771 // find and register all GRpcService-enabled beans
48- for (BindableService bindableService : getTypedBeansWithAnnotation (GRpcService .class ,BindableService .class )) {
72+ for (NamedBeanWrapper < BindableService > bindableService : getTypedBeansWithAnnotation (GRpcService .class ,BindableService .class )) {
4973
50- ServerServiceDefinition serviceDefinition = bindableService .bindService ();
51- GRpcService gRpcServiceAnn = bindableService . getClass (). getAnnotation ( GRpcService .class );
74+ ServerServiceDefinition serviceDefinition = bindableService .getBean (). bindService ();
75+ GRpcService gRpcServiceAnn = applicationContext . findAnnotationOnBean ( bindableService . getBeanName (), GRpcService .class );
5276 serviceDefinition = bindInterceptors (serviceDefinition ,gRpcServiceAnn ,globalInterceptors );
5377 serverBuilder .addService (serviceDefinition );
5478 log .info ("'{}' service has been registered." , bindableService .getClass ().getName ());
@@ -107,7 +131,7 @@ public void destroy() throws Exception {
107131 log .info ("gRPC server stopped." );
108132 }
109133
110- private <T > Collection <T > getTypedBeansWithAnnotation (Class <? extends Annotation > annotationType , Class <T > beanType ) throws Exception {
134+ private <T > Collection <NamedBeanWrapper < T > > getTypedBeansWithAnnotation (Class <? extends Annotation > annotationType , Class <T > beanType ) throws Exception {
111135
112136
113137 return Stream .of (applicationContext .getBeanNamesForType (beanType ))
@@ -119,7 +143,7 @@ private <T> Collection<T> getTypedBeansWithAnnotation(Class<? extends Annotation
119143 }
120144 return null != applicationContext .getBeanFactory ().findAnnotationOnBean (name ,annotationType );
121145 })
122- .map (name -> applicationContext .getBeanFactory ().getBean (name ,beanType ))
146+ .map (name -> new NamedBeanWrapper < T >( name , applicationContext .getBeanFactory ().getBean (name ,beanType )) )
123147 .collect (Collectors .toList ());
124148
125149 }
0 commit comments