Fix @ServiceConnection in native tests
Using `@ServiceConnection` results in the definition of one or more connection details beans. These bean definitions use an instance supplier which is not supported by AOT. This results in a failure during AOT processing. This commit introduces a BeanRegistrationExcludeFilter to exclude from AOT processing the beans created from a `@ServiceConnection`. They are not needed as the registrar will run again in the native image and define the beans at which point the use of an instance supplier is supported again. Fixes gh-35663
This commit is contained in:
@@ -28,7 +28,9 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactories;
|
||||
@@ -102,7 +104,9 @@ class ConnectionDetailsRegistrar {
|
||||
Class<T> beanType = (Class<T>) connectionDetails.getClass();
|
||||
Supplier<T> beanSupplier = () -> (T) connectionDetails;
|
||||
logger.debug(LogMessage.of(() -> "Registering '%s' for %s".formatted(beanName, source)));
|
||||
registry.registerBeanDefinition(beanName, new RootBeanDefinition(beanType, beanSupplier));
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType, beanSupplier);
|
||||
beanDefinition.setAttribute(ServiceConnection.class.getName(), true);
|
||||
registry.registerBeanDefinition(beanName, beanDefinition);
|
||||
}
|
||||
|
||||
private String getBeanName(ContainerConnectionSource<?> source, ConnectionDetails connectionDetails) {
|
||||
@@ -113,4 +117,13 @@ class ConnectionDetailsRegistrar {
|
||||
return StringUtils.uncapitalize(parts.stream().map(StringUtils::capitalize).collect(Collectors.joining()));
|
||||
}
|
||||
|
||||
class ServiceConnectionBeanRegistrationExcludeFilter implements BeanRegistrationExcludeFilter {
|
||||
|
||||
@Override
|
||||
public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
|
||||
return registeredBean.getMergedBeanDefinition().getAttribute(ServiceConnection.class.getName()) != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=\
|
||||
org.springframework.boot.testcontainers.service.connection.ConnectionDetailsRegistrar.ServiceConnectionBeanRegistrationExcludeFilter
|
||||
Reference in New Issue
Block a user