Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.ClassUtils;
import org.springframework.grpc.server.GrpcServerFactory;
import org.springframework.grpc.server.InProcessGrpcServerFactory;
import org.springframework.grpc.server.lifecycle.GrpcServerStartedEvent;
Expand All @@ -50,8 +51,13 @@
class GrpcPortInfoApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private static final String GRPC_SERVER_STARTED_EVENT = "org.springframework.grpc.server.lifecycle.GrpcServerStartedEvent";

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
if (!ClassUtils.isPresent(GRPC_SERVER_STARTED_EVENT, applicationContext.getClassLoader())) {
return;
}
applicationContext.addApplicationListener(new Listener(applicationContext));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package org.springframework.boot.grpc.test.autoconfigure;

import java.io.IOException;

import io.grpc.Server;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -43,6 +46,29 @@ class GrpcPortInfoApplicationContextInitializerTests {

private static final String PORT_PROPERTY = "local.grpc.server.port";

@Test
void whenGrpcServerStartedEventIsNotOnTheClasspathRegistersNoListener() throws IOException {
// gh-50825: the initializer is registered unconditionally via
// spring.factories, so with spring-grpc absent the listener's generic
// interface cannot be resolved and Spring throws TypeNotPresentException.
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
FilteredClassLoader classLoader = new FilteredClassLoader(GrpcServerStartedEvent.class)) {
context.setClassLoader(classLoader);
int before = context.getApplicationListeners().size();
new GrpcPortInfoApplicationContextInitializer().initialize(context);
assertThat(context.getApplicationListeners()).hasSize(before);
}
}

@Test
void whenGrpcServerStartedEventIsOnTheClasspathRegistersListener() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
int before = context.getApplicationListeners().size();
new GrpcPortInfoApplicationContextInitializer().initialize(context);
assertThat(context.getApplicationListeners()).hasSize(before + 1);
}
}

@Test
void whenServerHasAddressInitializerSetsPortProperty() {
NettyGrpcServerFactory factory = mock();
Expand Down