Skip to content

Commit c3b6aad

Browse files
authored
Merge pull request #532 from yidongnan/fix/client-interceptor-annotation
Fix GrpcClient#interceptors bean lookup + improve examples
2 parents d2d50ed + 83942b6 commit c3b6aad

File tree

7 files changed

+166
-10
lines changed

7 files changed

+166
-10
lines changed

docs/en/client/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ The following examples demonstrate how to use annotations to create a global cli
154154
public class ThirdPartyInterceptorConfig {}
155155

156156
@GrpcGlobalServerInterceptor
157-
ServerInterceptor logServerInterceptor() {
157+
LogGrpcInterceptor logServerInterceptor() {
158158
return new LogGrpcInterceptor();
159159
}
160160

examples/cloud-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.Order;
2323

24-
import io.grpc.ClientInterceptor;
2524
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2625

2726
@Order(Ordered.LOWEST_PRECEDENCE)
2827
@Configuration(proxyBeanMethods = false)
2928
public class GlobalClientInterceptorConfiguration {
3029

3130
@GrpcGlobalClientInterceptor
32-
ClientInterceptor logClientInterceptor() {
31+
LogGrpcInterceptor logClientInterceptor() {
3332
return new LogGrpcInterceptor();
3433
}
3534

examples/cloud-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import org.springframework.context.annotation.Configuration;
2121

22-
import io.grpc.ServerInterceptor;
2322
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
2423

2524
@Configuration(proxyBeanMethods = false)
2625
public class GlobalInterceptorConfiguration {
2726

2827
@GrpcGlobalServerInterceptor
29-
ServerInterceptor logServerInterceptor() {
28+
LogGrpcInterceptor logServerInterceptor() {
3029
return new LogGrpcInterceptor();
3130
}
3231

examples/local-grpc-client/src/main/java/net/devh/boot/grpc/examples/local/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.Order;
2323

24-
import io.grpc.ClientInterceptor;
2524
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2625

2726
@Order(Ordered.LOWEST_PRECEDENCE)
2827
@Configuration(proxyBeanMethods = false)
2928
public class GlobalClientInterceptorConfiguration {
3029

3130
@GrpcGlobalClientInterceptor
32-
ClientInterceptor logClientInterceptor() {
31+
LogGrpcInterceptor logClientInterceptor() {
3332
return new LogGrpcInterceptor();
3433
}
3534

examples/local-grpc-server/src/main/java/net/devh/boot/grpc/examples/local/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import org.springframework.context.annotation.Configuration;
2121

22-
import io.grpc.ServerInterceptor;
2322
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
2423

2524
@Configuration(proxyBeanMethods = false)
2625
public class GlobalInterceptorConfiguration {
2726

2827
@GrpcGlobalServerInterceptor
29-
ServerInterceptor logServerInterceptor() {
28+
LogGrpcInterceptor logServerInterceptor() {
3029
return new LogGrpcInterceptor();
3130
}
3231

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected List<ClientInterceptor> interceptorsFromAnnotation(final GrpcClient an
180180
final List<ClientInterceptor> list = Lists.newArrayList();
181181
for (final Class<? extends ClientInterceptor> interceptorClass : annotation.interceptors()) {
182182
final ClientInterceptor clientInterceptor;
183-
if (this.applicationContext.getBeanNamesForType(ClientInterceptor.class).length > 0) {
183+
if (this.applicationContext.getBeanNamesForType(interceptorClass).length > 0) {
184184
clientInterceptor = this.applicationContext.getBean(interceptorClass);
185185
} else {
186186
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2016-2021 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.client.inject;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
23+
import java.lang.annotation.Annotation;
24+
import java.util.List;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.test.annotation.DirtiesContext;
32+
33+
import io.grpc.CallOptions;
34+
import io.grpc.Channel;
35+
import io.grpc.ClientCall;
36+
import io.grpc.ClientInterceptor;
37+
import io.grpc.MethodDescriptor;
38+
import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration;
39+
40+
/**
41+
* Tests for {@link GrpcClientBeanPostProcessor}.
42+
*
43+
* @author Daniel Theuke ([email protected])
44+
*/
45+
@SpringBootTest(classes = GrpcClientBeanPostProcessorTest.TestConfig.class)
46+
@DirtiesContext
47+
@ImportAutoConfiguration(GrpcClientAutoConfiguration.class)
48+
class GrpcClientBeanPostProcessorTest {
49+
50+
@Autowired
51+
GrpcClientBeanPostProcessor postProcessor;
52+
53+
@Autowired
54+
Interceptor1 interceptor1;
55+
56+
@Test
57+
void testInterceptorsFromAnnotation1() {
58+
final List<ClientInterceptor> beans =
59+
assertDoesNotThrow(() -> this.postProcessor.interceptorsFromAnnotation(new GrpcClient() {
60+
61+
@Override
62+
public Class<? extends Annotation> annotationType() {
63+
return GrpcClient.class;
64+
}
65+
66+
@Override
67+
public String value() {
68+
return "test";
69+
}
70+
71+
@Override
72+
public boolean sortInterceptors() {
73+
return false;
74+
}
75+
76+
@Override
77+
@SuppressWarnings("unchecked")
78+
public Class<? extends ClientInterceptor>[] interceptors() {
79+
return new Class[] {Interceptor1.class};
80+
}
81+
82+
@Override
83+
public String[] interceptorNames() {
84+
return new String[0];
85+
}
86+
87+
}));
88+
89+
assertThat(beans).containsExactly(this.interceptor1);
90+
}
91+
92+
@Test
93+
void testInterceptorsFromAnnotation2() {
94+
final List<ClientInterceptor> beans =
95+
assertDoesNotThrow(() -> this.postProcessor.interceptorsFromAnnotation(new GrpcClient() {
96+
97+
@Override
98+
public Class<? extends Annotation> annotationType() {
99+
return GrpcClient.class;
100+
}
101+
102+
@Override
103+
public String value() {
104+
return "test";
105+
}
106+
107+
@Override
108+
public boolean sortInterceptors() {
109+
return false;
110+
}
111+
112+
@Override
113+
@SuppressWarnings("unchecked")
114+
public Class<? extends ClientInterceptor>[] interceptors() {
115+
return new Class[] {Interceptor2.class};
116+
}
117+
118+
@Override
119+
public String[] interceptorNames() {
120+
return new String[0];
121+
}
122+
123+
}));
124+
125+
assertThat(beans).hasSize(1).doesNotContain(this.interceptor1);
126+
}
127+
128+
static class TestConfig {
129+
130+
@Bean
131+
Interceptor1 interceptor1() {
132+
return new Interceptor1();
133+
}
134+
135+
136+
}
137+
138+
public static class Interceptor1 implements ClientInterceptor {
139+
140+
@Override
141+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
142+
final MethodDescriptor<ReqT, RespT> method,
143+
final CallOptions callOptions, final Channel next) {
144+
return next.newCall(method, callOptions);
145+
}
146+
147+
}
148+
149+
public static class Interceptor2 implements ClientInterceptor {
150+
151+
@Override
152+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
153+
final MethodDescriptor<ReqT, RespT> method,
154+
final CallOptions callOptions, final Channel next) {
155+
return next.newCall(method, callOptions);
156+
}
157+
158+
}
159+
160+
}

0 commit comments

Comments
 (0)