Skip to content

Commit 421120c

Browse files
author
Alexander Furer
committed
code refactoring
1 parent 95b0cb4 commit 421120c

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTestAop.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class DemoAppTestAop {
3434

3535

3636
@Test
37-
public void simpleGreeting() throws ExecutionException, InterruptedException {
37+
public void simpleAopTest() throws ExecutionException, InterruptedException {
3838

3939
assertTrue(AopUtils.isAopProxy(greeterService));
4040
assertTrue(AopUtils.isAopProxy(calculatorService));

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcServerRunner.java

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,7 @@
2424
@Slf4j
2525
public class GRpcServerRunner implements CommandLineRunner,DisposableBean {
2626

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-
}
27+
5228

5329
@Autowired
5430
private AbstractApplicationContext applicationContext;
@@ -62,22 +38,24 @@ public void setBeanName(String beanName) {
6238
public void run(String... args) throws Exception {
6339
log.info("Starting gRPC Server ...");
6440

65-
Collection<ServerInterceptor> globalInterceptors = getTypedBeansWithAnnotation(GRpcGlobalInterceptor.class,ServerInterceptor.class)
66-
.stream()
67-
.map(NamedBeanWrapper::getBean)
41+
Collection<ServerInterceptor> globalInterceptors = getBeanNamesByTypeWithAnnotation(GRpcGlobalInterceptor.class,ServerInterceptor.class)
42+
.map(name -> applicationContext.getBeanFactory().getBean(name,ServerInterceptor.class))
6843
.collect(Collectors.toList());
44+
6945
final ServerBuilder<?> serverBuilder = ServerBuilder.forPort(gRpcServerProperties.getPort());
7046

7147
// find and register all GRpcService-enabled beans
72-
for(NamedBeanWrapper<BindableService> bindableService : getTypedBeansWithAnnotation(GRpcService.class,BindableService.class)) {
48+
getBeanNamesByTypeWithAnnotation(GRpcService.class,BindableService.class)
49+
.forEach(name->{
50+
BindableService srv = applicationContext.getBeanFactory().getBean(name, BindableService.class);
51+
ServerServiceDefinition serviceDefinition = srv.bindService();
52+
GRpcService gRpcServiceAnn = applicationContext.findAnnotationOnBean(name,GRpcService.class);
53+
serviceDefinition = bindInterceptors(serviceDefinition,gRpcServiceAnn,globalInterceptors);
54+
serverBuilder.addService(serviceDefinition);
55+
log.info("'{}' service has been registered.", srv.getClass().getName());
7356

74-
ServerServiceDefinition serviceDefinition = bindableService.getBean().bindService();
75-
GRpcService gRpcServiceAnn = applicationContext.findAnnotationOnBean(bindableService.getBeanName(),GRpcService.class);
76-
serviceDefinition = bindInterceptors(serviceDefinition,gRpcServiceAnn,globalInterceptors);
77-
serverBuilder.addService(serviceDefinition);
78-
log.info("'{}' service has been registered.", bindableService.getClass().getName());
57+
});
7958

80-
}
8159

8260
server = serverBuilder.build().start();
8361
log.info("gRPC Server started, listening on port {}.", gRpcServerProperties.getPort());
@@ -131,7 +109,7 @@ public void destroy() throws Exception {
131109
log.info("gRPC server stopped.");
132110
}
133111

134-
private <T> Collection<NamedBeanWrapper<T>> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception{
112+
private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception{
135113

136114

137115
return Stream.of(applicationContext.getBeanNamesForType(beanType))
@@ -142,10 +120,7 @@ private <T> Collection<NamedBeanWrapper<T>> getTypedBeansWithAnnotation(Class<?
142120
return metadata.isAnnotated(annotationType.getName());
143121
}
144122
return null!= applicationContext.getBeanFactory().findAnnotationOnBean(name,annotationType);
145-
})
146-
.map(name -> new NamedBeanWrapper<T>(name,applicationContext.getBeanFactory().getBean(name,beanType)) )
147-
.collect(Collectors.toList());
148-
123+
});
149124
}
150125

151126

0 commit comments

Comments
 (0)