Skip to content

Commit 23d9dfe

Browse files
committed
Add @GrpcClientBean to docs, removed inline imports
1 parent 340fe8f commit 23d9dfe

File tree

4 files changed

+141
-4
lines changed

4 files changed

+141
-4
lines changed

docs/en/client/configuration.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This section describes how you can configure your grpc-spring-boot-starter clien
99
- [Configuration via Properties](#configuration-via-properties)
1010
- [Choosing the Target](#choosing-the-target)
1111
- [Configuration via Beans](#configuration-via-beans)
12+
- [GrpcClientBean](#grpcclientbean)
13+
- [GrpcClientBeans](#grpcclientbeans)
1214
- [GrpcChannelConfigurer](#grpcchannelconfigurer)
1315
- [ClientInterceptor](#clientinterceptor)
1416
- [StubFactory](#stubfactory)
@@ -115,6 +117,93 @@ First of all most of the beans can be replaced by custom ones, that you can conf
115117
If you don't wish to go that far, you can use classes such as `GrpcChannelConfigurer` and `StubTransformer` to configure
116118
the channels, stubs and other components without losing the features provided by this library.
117119

120+
### GrpcClientBean
121+
122+
This should significantly help with ``@Autowire`` and ``@Qualifier`` because default annotation``@GrpcClient`` is
123+
not designed for usage with spring 'injection' annotations.
124+
``@GrpcClientBean`` require annotation ``@Cofiguration`` or inherited, for example ``@TestConfiguraiton``.
125+
It is definitely not recommended using ``@GrpcClientBean`` and field annotated with ``@GrpcClient`` for same
126+
configuration class, but it`s still possible.
127+
128+
````java
129+
@Configuration
130+
@GrpcClientBean(
131+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
132+
beanName = "blockingStub",
133+
client = @GrpcClient("test")
134+
)
135+
public static class YourCustomConfiguration {
136+
137+
@Bean
138+
FoobarService(@Autowired TestServiceGrpc.TestServiceBlockingStub blockingStub) {
139+
return new FoobarService(blockingStub);
140+
}
141+
}
142+
143+
@Service
144+
@AllArgsConsturtor
145+
public class FoobarService {
146+
private TestServiceBlockingStub blockingStub;
147+
148+
public String receiveGreeting(String name) {
149+
HelloRequest request = HelloRequest.newBuilder()
150+
.setName(name)
151+
.build();
152+
return blockingStub.sayHello(request).getMessage();
153+
}
154+
155+
}
156+
````
157+
158+
### GrpcClientBeans
159+
160+
``@GrpcClientBeans`` designed for registration multiple stubs to the spring context from single configuration class.
161+
162+
````java
163+
@Configuration
164+
@GrpcClientBeans(value = {
165+
@GrpcClientBean(
166+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
167+
beanName = "blockingStub",
168+
client = @GrpcClient("test")),
169+
@GrpcClientBean(
170+
clazz = TestServiceGrpc.FactoryMethodAccessibleStub.class,
171+
beanName = "accessibleStub",
172+
client = @GrpcClient("test"))
173+
})
174+
public static class YourCustomConfiguration {
175+
176+
@Bean
177+
FoobarService(@Autowired TestServiceGrpc.TestServiceBlockingStub blockingStub,
178+
@Autowired TestServiceGrpc.FactoryMethodAccessibleStub accessibleStub) {
179+
return new FoobarService(blockingStub, accessibleStub);
180+
}
181+
}
182+
183+
@Service
184+
@AllArgsConsturtor
185+
public class FoobarService {
186+
187+
private TestServiceBlockingStub blockingStub;
188+
private FactoryMethodAccessibleStub accessibleStub;
189+
190+
public String receiveGreeting(String name) {
191+
HelloRequest request = HelloRequest.newBuilder()
192+
.setName(name)
193+
.build();
194+
return blockingStub.sayHello(request).getMessage();
195+
}
196+
197+
public String receiveAnotherGreeting(String name) {
198+
HelloRequest request = HelloRequest.newBuilder()
199+
.setName(name)
200+
.build();
201+
return accessibleStub.sayHi(request).getMessage();
202+
}
203+
204+
}
205+
````
206+
118207
### GrpcChannelConfigurer
119208

120209
The grpc client configurer allows you to add your custom configuration to grpc's `ManagedChannelBuilder`s.

docs/en/client/getting-started.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ If you don't wish to use any advanced features, then the first element is probab
106106
The annotation that marks fields and setters for auto injection of clients.
107107
Supports `Channel`s, and all kinds of `Stub`s.
108108
Do not use `@GrpcClient` in conjunction with `@Autowired` or `@Inject`.
109-
Currently it isn't supported for constructor and `@Bean` method parameters. \
109+
Currently, it isn't supported for constructor and `@Bean` method parameters. For this case look below
110+
to the``@GrpcClientBean``. \
110111
**Note:** Services provided by the same application can only be accessed/called in/after the
111112
`ApplicationStartedEvent`. Stubs connecting to services outside of the application can be used earlier; starting with
112113
`@PostConstruct` / `InitializingBean#afterPropertiesSet()`.
114+
- [`@GrpcClientBean`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/inject/GrpcClientBean.html):
115+
The annotation should help with registration ``@GrpcClient`` to the spring context for usage with ``@Autowire`` and
116+
``@Qualifier``, required annotation ``@Configuration`` or inherited, for example ``@TestConfiguration``.
117+
- [`@GrpcClientBeans`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/inject/GrpcClientBeans.html):
118+
The annotation should help with multiple registration ``@GrpcClient`` to the spring context.
113119
- [`Channel`](https://javadoc.io/page/io.grpc/grpc-all/latest/io/grpc/Channel.html):
114120
The Channel is a connection pool for a single address. The target servers might serve multiple grpc-services though.
115121
The address will be resolved using a `NameResolver` and might point to a fixed or dynamic number of servers.
@@ -167,13 +173,48 @@ public class FoobarService {
167173
public String receiveGreeting(String name) {
168174
HelloRequest request = HelloRequest.newBuilder()
169175
.setName(name)
170-
.build()
176+
.build();
171177
return myServiceStub.sayHello(request).getMessage();
172178
}
173179

174180
}
175181
````
176182

183+
Also you can feel free to inject stub with ``@GrpcClientBean`` with ``@Configuration`` for more wide usage in
184+
another services.
185+
186+
> **Note:** It is definitely not recommended using ``@GrpcClientBean`` and field annotated with ``@GrpcClient`` like in
187+
> the first example for same configuration class, but it`s still possible.
188+
189+
````java
190+
@Configuration
191+
@GrpcClientBean(
192+
clazz = TestServiceGrpc.TestServiceBlockingStub.class,
193+
beanName = "blockingStub",
194+
client = @GrpcClient("test")
195+
)
196+
public static class YourCustomConfiguration {
197+
198+
@Bean
199+
FoobarService(@Autowired TestServiceGrpc.TestServiceBlockingStub blockingStub) {
200+
return new FoobarService(blockingStub);
201+
}
202+
}
203+
204+
@Service
205+
@AllArgsConsturtor
206+
public class FoobarService {
207+
private TestServiceBlockingStub blockingStub;
208+
209+
public String receiveGreeting(String name) {
210+
HelloRequest request = HelloRequest.newBuilder()
211+
.setName(name)
212+
.build();
213+
return blockingStub.sayHello(request).getMessage();
214+
}
215+
216+
}
217+
````
177218
## Additional Topics <!-- omit in toc -->
178219

179220
- *Getting Started*

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package net.devh.boot.grpc.client.inject;
1919

20-
import java.lang.annotation.*;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Repeatable;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
2125

2226
/**
2327
* Annotation that can be added to `@Configuration` classes to create GrpcClient bean in the ApplicationContext.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package net.devh.boot.grpc.client.inject;
1919

20-
import java.lang.annotation.*;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
2124

2225
/**
2326
* Annotation that can be added to `@Configuration` classes to create `GrpcClientBean` beans in the ApplicationContext.

0 commit comments

Comments
 (0)