GH-936 - Make sure ModuleTracingBeanPostProcessor is executed early.

We now explicitly declare an order for the MTBPP so that it gets registered for execution before the ones that create infrastructure for message listeners so that those already see the potentially proxied instance and invocations actually invoke the advice creating the traces.
This commit is contained in:
Oliver Drotbohm
2024-11-19 23:50:45 +01:00
parent 4218dffc9d
commit 85c5206cd3
3 changed files with 29 additions and 1 deletions

View File

@@ -99,6 +99,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -33,6 +33,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.util.Assert;
@@ -42,7 +43,7 @@ import org.springframework.util.Assert;
*
* @author Oliver Drotbohm
*/
public class ModuleTracingBeanPostProcessor extends ModuleTracingSupport implements BeanPostProcessor {
public class ModuleTracingBeanPostProcessor extends ModuleTracingSupport implements BeanPostProcessor, Ordered {
public static final String MODULE_BAGGAGE_KEY = "org.springframework.modulith.module";
@@ -70,6 +71,15 @@ public class ModuleTracingBeanPostProcessor extends ModuleTracingSupport impleme
this.factory = factory;
}
/*
* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 50;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)

View File

@@ -22,6 +22,7 @@ import static org.mockito.Mockito.*;
import example.sample.SampleProperties;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
@@ -50,4 +51,15 @@ class ModuleTracingBeanPostProcessorUnitTests {
assertThat(result).isSameAs(bean);
}
@Test // GH-936
void processorIsRegisteredBefore() {
var rabbitProcessor = new RabbitListenerAnnotationBeanPostProcessor();
var tracingProcessor = new ModuleTracingBeanPostProcessor(mock(ApplicationModulesRuntime.class), () -> null,
new DefaultListableBeanFactory());
assertThat(rabbitProcessor.getOrder()).isGreaterThan(tracingProcessor.getOrder());
}
}