diff --git a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java index d6052094..91ed5dd7 100644 --- a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java +++ b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java @@ -2,10 +2,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -import io.grpc.BindableService; -import io.grpc.protobuf.services.ProtoReflectionService; @SpringBootApplication public class GrpcServerApplication { @@ -14,9 +10,4 @@ public static void main(String[] args) { SpringApplication.run(GrpcServerApplication.class, args); } - @Bean - public BindableService serverReflection() { - return ProtoReflectionService.newInstance(); - } - } diff --git a/samples/grpc-server/src/main/resources/application.properties b/samples/grpc-server/src/main/resources/application.properties index 566605a7..0e228bd7 100644 --- a/samples/grpc-server/src/main/resources/application.properties +++ b/samples/grpc-server/src/main/resources/application.properties @@ -1 +1,2 @@ spring.application.name=grpc-server +spring.grpc.server.reflection.enabled=true \ No newline at end of file diff --git a/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java index d6052094..5cf22b1a 100644 --- a/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java +++ b/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java @@ -2,10 +2,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -import io.grpc.BindableService; -import io.grpc.protobuf.services.ProtoReflectionService; @SpringBootApplication public class GrpcServerApplication { @@ -13,10 +9,4 @@ public class GrpcServerApplication { public static void main(String[] args) { SpringApplication.run(GrpcServerApplication.class, args); } - - @Bean - public BindableService serverReflection() { - return ProtoReflectionService.newInstance(); - } - } diff --git a/spring-grpc-spring-boot-autoconfigure/pom.xml b/spring-grpc-spring-boot-autoconfigure/pom.xml index 35521a06..61dbbf0a 100644 --- a/spring-grpc-spring-boot-autoconfigure/pom.xml +++ b/spring-grpc-spring-boot-autoconfigure/pom.xml @@ -60,6 +60,11 @@ grpc-servlet-jakarta true + + io.grpc + grpc-services + true + io.grpc grpc-netty-shaded @@ -89,6 +94,6 @@ test - + \ No newline at end of file diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java index 0c5df785..1b0952de 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java @@ -23,6 +23,7 @@ import io.grpc.servlet.jakarta.ServletServerBuilder; import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -48,6 +49,7 @@ * @author Toshiaki Maki */ @AutoConfiguration +@AutoConfigureAfter(GrpcServerReflectionAutoConfiguration.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @ConditionalOnClass(BindableService.class) @ConditionalOnBean(BindableService.class) diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfiguration.java new file mode 100644 index 00000000..3a670af3 --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfiguration.java @@ -0,0 +1,31 @@ +package org.springframework.grpc.autoconfigure.server; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; + +import io.grpc.BindableService; +import io.grpc.protobuf.services.ProtoReflectionService; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for gRPC Reflection service + *

+ * This auto-configuration is enabled by default. To disable it, set the configuration + * flag {spring.grpc.server.reflection.enabled=false} in your application properties. + * auto-configuration to execute + * + * @author Haris Zujo + */ +@AutoConfiguration +@ConditionalOnClass(ProtoReflectionService.class) +@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true) +public class GrpcServerReflectionAutoConfiguration { + + @Bean + public BindableService serverReflection() { + return ProtoReflectionService.newInstance(); + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 7102b598..ceb06f05 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -4,6 +4,10 @@ { "name": "spring.grpc.server.port", "defaultValue": "9090" + }, + { + "name": "spring.grpc.server.reflection.enabled", + "defaultValue": "true" } ] } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index a7e2a86e..8322bec2 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,5 @@ org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration +org.springframework.grpc.autoconfigure.server.GrpcServerReflectionAutoConfiguration + diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java new file mode 100644 index 00000000..5e4d37cb --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java @@ -0,0 +1,40 @@ +package org.springframework.grpc.autoconfigure.server; + +import io.grpc.BindableService; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GrpcServerReflectionAutoConfigurationTests { + + private ApplicationContextRunner contextRunner() { + return new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(GrpcServerReflectionAutoConfiguration.class)) + .withBean("noopServerLifecylcle", GrpcServerLifecycle.class, Mockito::mock); + } + + @Test + void whenReflectionEnabledFlagNotPresentThenCreateDefaultBean() { + this.contextRunner() + .run((context -> assertThat(context).hasSingleBean(BindableService.class))); + } + + @Test + void whenReflectionEnabledThenCreateBean() { + this.contextRunner() + .withPropertyValues("spring.grpc.server.reflection.enabled=true") + .run((context) -> assertThat(context).hasSingleBean(BindableService.class)); + } + + @Test + void whenReflectionDisabledThenSkipBeanCreation() { + this.contextRunner() + .withPropertyValues("spring.grpc.server.reflection.enabled=false") + .run((context) -> assertThat(context).doesNotHaveBean(BindableService.class)); + } + +}