Rename annotation to mirror Spring HTTP interfaces

This commit is contained in:
Dave Syer
2025-03-26 08:30:01 +00:00
parent 55bd93958a
commit 8b26b8f262
7 changed files with 18 additions and 18 deletions

View File

@@ -21,7 +21,7 @@ import org.springframework.experimental.boot.server.exec.MavenClasspathEntry;
import org.springframework.experimental.boot.test.context.EnableDynamicProperty;
import org.springframework.experimental.boot.test.context.OAuth2ClientProviderIssuerUri;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcClient;
import org.springframework.grpc.client.ImportGrpcClients;
import org.springframework.grpc.client.GrpcClientRegistryCustomizer;
import org.springframework.grpc.client.interceptor.security.BearerTokenAuthenticationInterceptor;
import org.springframework.grpc.sample.proto.HelloReply;
@@ -113,7 +113,7 @@ public class GrpcServerApplicationTests {
@TestConfiguration(proxyBeanMethods = false)
@EnableDynamicProperty
@GrpcClient(target = "stub",
@ImportGrpcClients(target = "stub",
types = { SimpleGrpc.SimpleBlockingStub.class, ServerReflectionGrpc.ServerReflectionStub.class })
static class ExtraConfiguration {

View File

@@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.grpc.client.GrpcClient;
import org.springframework.grpc.client.ImportGrpcClients;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.ReactorSimpleGrpc;
@@ -46,7 +46,7 @@ public class GrpcServerApplicationTests {
}
@TestConfiguration
@GrpcClient(types = ReactorSimpleGrpc.ReactorSimpleStub.class)
@ImportGrpcClients(types = ReactorSimpleGrpc.ReactorSimpleStub.class)
static class ExtraConfiguration {
}

View File

@@ -17,7 +17,7 @@ import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.grpc.client.BlockingStubFactory;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcClient;
import org.springframework.grpc.client.ImportGrpcClients;
import org.springframework.grpc.client.GrpcClientRegistryCustomizer;
import org.springframework.grpc.client.interceptor.security.BasicAuthenticationInterceptor;
import org.springframework.grpc.sample.proto.HelloReply;
@@ -108,7 +108,7 @@ public class GrpcServerApplicationTests {
}
@TestConfiguration
@GrpcClient(target = "stub", prefix = "unsecured",
@ImportGrpcClients(target = "stub", prefix = "unsecured",
types = { SimpleGrpc.SimpleBlockingStub.class, ServerReflectionGrpc.ServerReflectionStub.class })
static class ExtraConfiguration {

View File

@@ -29,8 +29,8 @@ public class GrpcClientConfiguration implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) {
Set<AnnotationAttributes> attrs = meta.getMergedRepeatableAnnotationAttributes(GrpcClient.class,
GrpcClient.Container.class, false);
Set<AnnotationAttributes> attrs = meta.getMergedRepeatableAnnotationAttributes(ImportGrpcClients.class,
ImportGrpcClients.Container.class, false);
for (AnnotationAttributes attr : attrs) {
register(registry, meta, attr, meta.getClassName() + ".");
}

View File

@@ -37,8 +37,8 @@ import io.grpc.stub.AbstractStub;
@Target(ElementType.TYPE)
@Documented
@Import(GrpcClientConfiguration.class)
@Repeatable(GrpcClient.Container.class)
public @interface GrpcClient {
@Repeatable(ImportGrpcClients.Container.class)
public @interface ImportGrpcClients {
/**
* The name or base URL of the gRPC server to connect to. If not specified, the client
@@ -87,7 +87,7 @@ public @interface GrpcClient {
@Documented
@interface Container {
GrpcClient[] value() default {};
ImportGrpcClients[] value() default {};
}

View File

@@ -35,7 +35,7 @@ Spring gRPC will scan the application packages for gRPC stubs and automatically
=== Package Scanning
The `@GrpcClient` annotation can be used to control the scan for gRPC stub implementations.
The `@ImportGrpcClients` annotation can be used to control the scan for gRPC stub implementations.
To scan a package you can specify the `basePackages` or `basePackageClasses` attribute.
Then elsewhere in the application you can `@Autowired` the generated gRPC stubs (the blocking sub-type by default).
You can change the factory used to create the stubs from `BlockingStubFactory` by setting the `factory` attribute.
@@ -46,7 +46,7 @@ The default behaviour in a Spring Boot application is equivalent to the followin
[source,java]
----
// This is the default behaviour, so not necessary to add this annotation unless you change its attributes
@EnableGrpcClients(@GrpcClient(basePackageClasses = MyApplication.class))
@ImportGrpcClients(basePackageClasses = MyApplication.class)
@SpringBootApplication
class MyApplication {
// ...
@@ -58,8 +58,8 @@ The customizer has full control over the scanning and registration of the gRPC c
=== Register Individual Stub Types
The `@GrpcClient` has a `types` attribute if you want to register specific stub types instead of scanning a package.
A `GrpcClientRegistryCustomizer` can also be used to control the registration of the gRPC clients in the application context, and the API is flexible enough to allow you to add your own behaviour that would not be possible with just the `@GrpcClient` annotation.
The `@ImportGrpcClients` has a `types` attribute if you want to register specific stub types instead of scanning a package.
A `GrpcClientRegistryCustomizer` can also be used to control the registration of the gRPC clients in the application context, and the API is flexible enough to allow you to add your own behaviour that would not be possible with just the `@ImportGrpcClients` annotation.
For example, to add just one client stub using the default channel:
[source,java]
@@ -98,7 +98,7 @@ The customizer has to run very early in the application lifecycle, so you always
== Create a Client Manually
Instead of using the `@GrpcClient` or `GrpcClientRegistry` features, we can create a client `@Bean` manually.
Instead of using the `@ImportGrpcClients` or `GrpcClientRegistry` features, we can create a client `@Bean` manually.
The most common usage of a channel is to create a client that binds to a service.
For example:

View File

@@ -27,13 +27,13 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.ChannelConfig;
import org.springframework.grpc.client.BlockingStubFactory;
import org.springframework.grpc.client.GrpcClient;
import org.springframework.grpc.client.ImportGrpcClients;
import org.springframework.grpc.client.GrpcClientRegistryCustomizer;
import org.springframework.grpc.client.GrpcClientRegistryPostProcessor;
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(GrpcClientRegistryPostProcessor.class)
@GrpcClient
@ImportGrpcClients
public class ClientScanConfiguration {
@Bean