From 8b26b8f262809a10b47d5aea12b2df7d36d6af5c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 26 Mar 2025 08:30:01 +0000 Subject: [PATCH] Rename annotation to mirror Spring HTTP interfaces --- .../grpc/sample/GrpcServerApplicationTests.java | 4 ++-- .../grpc/sample/GrpcServerApplicationTests.java | 4 ++-- .../grpc/sample/GrpcServerApplicationTests.java | 4 ++-- .../grpc/client/GrpcClientConfiguration.java | 4 ++-- .../client/{GrpcClient.java => ImportGrpcClients.java} | 6 +++--- .../src/main/antora/modules/ROOT/pages/client.adoc | 10 +++++----- .../autoconfigure/client/ClientScanConfiguration.java | 4 ++-- 7 files changed, 18 insertions(+), 18 deletions(-) rename spring-grpc-core/src/main/java/org/springframework/grpc/client/{GrpcClient.java => ImportGrpcClients.java} (95%) diff --git a/samples/grpc-oauth2/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-oauth2/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index e26e413..6f3f852 100644 --- a/samples/grpc-oauth2/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-oauth2/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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 { diff --git a/samples/grpc-reactive/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-reactive/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 309695a..5954147 100644 --- a/samples/grpc-reactive/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-reactive/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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 { } diff --git a/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 82770b2..5be3ebb 100644 --- a/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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 { diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientConfiguration.java b/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientConfiguration.java index b73f3f0..b3d6a63 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientConfiguration.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientConfiguration.java @@ -29,8 +29,8 @@ public class GrpcClientConfiguration implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) { - Set attrs = meta.getMergedRepeatableAnnotationAttributes(GrpcClient.class, - GrpcClient.Container.class, false); + Set attrs = meta.getMergedRepeatableAnnotationAttributes(ImportGrpcClients.class, + ImportGrpcClients.Container.class, false); for (AnnotationAttributes attr : attrs) { register(registry, meta, attr, meta.getClassName() + "."); } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClient.java b/spring-grpc-core/src/main/java/org/springframework/grpc/client/ImportGrpcClients.java similarity index 95% rename from spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClient.java rename to spring-grpc-core/src/main/java/org/springframework/grpc/client/ImportGrpcClients.java index 0669f79..2bcfb13 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClient.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/client/ImportGrpcClients.java @@ -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 {}; } diff --git a/spring-grpc-docs/src/main/antora/modules/ROOT/pages/client.adoc b/spring-grpc-docs/src/main/antora/modules/ROOT/pages/client.adoc index a5693d9..8a3d9ab 100644 --- a/spring-grpc-docs/src/main/antora/modules/ROOT/pages/client.adoc +++ b/spring-grpc-docs/src/main/antora/modules/ROOT/pages/client.adoc @@ -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: diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java index 69f2910..0cc3074 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java @@ -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