Skip to content

Commit b12c228

Browse files
author
Alexander Furer
committed
fixes #28
1 parent d963075 commit b12c228

File tree

5 files changed

+116
-22
lines changed

5 files changed

+116
-22
lines changed

grpc-spring-boot-starter-demo/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ dependencies {
77
compile "io.grpc:grpc-netty:${grpcVersion}"
88
compile "org.springframework.boot:spring-boot-starter-actuator"
99
compile 'org.springframework.boot:spring-boot-starter-web'
10+
11+
1012
compile project(':grpc-spring-boot-starter')
1113

14+
testCompile 'org.springframework.boot:spring-boot-starter-aop'
1215
testCompile('org.springframework.boot:spring-boot-starter-test')
1316
}
1417
sourceSets {

grpc-spring-boot-starter-demo/src/main/protoGen/io/grpc/examples/GreeterGrpc.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
package io.grpc.examples;
22

3-
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
4-
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
5-
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
6-
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
7-
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
8-
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
9-
import static io.grpc.stub.ClientCalls.futureUnaryCall;
103
import static io.grpc.MethodDescriptor.generateFullMethodName;
4+
import static io.grpc.stub.ClientCalls.*;
115
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
12-
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
13-
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
14-
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
156
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
16-
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
177

188
/**
199
* <pre>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.lognet.springboot.grpc.demo.DemoApp;
6+
import org.springframework.aop.support.AopUtils;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.ActiveProfiles;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import java.util.concurrent.ExecutionException;
13+
14+
import static org.junit.Assert.assertTrue;
15+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
16+
17+
/**
18+
* Created by alexf on 28-Jan-16.
19+
*/
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(classes = {DemoApp.class },webEnvironment = DEFINED_PORT
22+
,properties = {"spring.aop.proxy-target-class=true"}
23+
)
24+
@ActiveProfiles(profiles = {"aopTest"})
25+
public class DemoAppTestAop {
26+
27+
28+
@Autowired
29+
private DemoApp.GreeterService greeterService;
30+
31+
@Autowired
32+
private DemoApp.CalculatorService calculatorService;
33+
34+
35+
36+
@Test
37+
public void simpleGreeting() throws ExecutionException, InterruptedException {
38+
39+
assertTrue(AopUtils.isAopProxy(greeterService));
40+
assertTrue(AopUtils.isAopProxy(calculatorService));
41+
42+
}
43+
44+
45+
46+
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import org.aspectj.lang.annotation.AfterReturning;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.springframework.context.annotation.Profile;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* Created by 310242212 on 01-Nov-16.
10+
*/
11+
12+
@Aspect
13+
@Component
14+
@Profile(value = {"aopTest"})
15+
public class AopServiceMonitor {
16+
17+
public AopServiceMonitor() {
18+
}
19+
20+
@AfterReturning("execution(* org.lognet..*Service.*(..))")
21+
public void logServiceAccess( ) {
22+
System.out.println("Completed: ");
23+
}
24+
25+
26+
27+
28+
29+
30+
}

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@
55
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
66
import org.springframework.beans.factory.BeanCreationException;
77
import org.springframework.beans.factory.DisposableBean;
8-
import org.springframework.beans.factory.InitializingBean;
98
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.beans.factory.config.BeanDefinition;
1110
import org.springframework.boot.CommandLineRunner;
12-
import org.springframework.context.ApplicationContext;
1311
import org.springframework.context.support.AbstractApplicationContext;
1412
import org.springframework.core.type.StandardMethodMetadata;
15-
import org.springframework.util.StreamUtils;
1613

1714
import java.lang.annotation.Annotation;
1815
import java.util.Collection;
1916
import java.util.List;
20-
import java.util.Map;
2117
import java.util.Optional;
22-
import java.util.function.Function;
2318
import java.util.stream.Collectors;
2419
import java.util.stream.Stream;
2520

@@ -29,6 +24,32 @@
2924
@Slf4j
3025
public 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

Comments
 (0)