Support for @LocalGrpcPort in @Lazy beans

This commit is contained in:
Dave Syer
2024-09-09 13:25:45 +01:00
parent e418bef34e
commit 2249f6e2b3
6 changed files with 150 additions and 3 deletions

View File

@@ -10,13 +10,15 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest
@SpringBootTest(properties = "spring.grpc.server.port=0")
public class GrpcServerApplicationTests {
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);
@@ -45,8 +47,9 @@ public class GrpcServerApplicationTests {
static class ExtraConfiguration {
@Bean
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels) {
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:9090").build());
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
}
}