From 8323476708bfee373f769df014889034e8ea74c4 Mon Sep 17 00:00:00 2001 From: Fury_Phoenix <64714532+Phoenix-Starlight@users.noreply.github.com> Date: Thu, 30 May 2024 02:18:58 -0700 Subject: [PATCH] Register ApplicationContext bean with class (#584) * Register ApplicationContext bean with class * Assert none of the beans' types are null * Fix typo * Refactor to use Micronaut application context * Use correct method to retrieve all bean defs --- .../spring/boot/starter/MicronautImportRegistrar.java | 1 + .../spring/boot/starter/EnableMicronautTest.java | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/spring-boot-starter/src/main/java/io/micronaut/spring/boot/starter/MicronautImportRegistrar.java b/spring-boot-starter/src/main/java/io/micronaut/spring/boot/starter/MicronautImportRegistrar.java index afa0b8b07..589a3ab89 100644 --- a/spring-boot-starter/src/main/java/io/micronaut/spring/boot/starter/MicronautImportRegistrar.java +++ b/spring-boot-starter/src/main/java/io/micronaut/spring/boot/starter/MicronautImportRegistrar.java @@ -130,6 +130,7 @@ public void registerBeanDefinitions( ppd.setBeanClass(MicronautPostProcess.class); ppd.setInstanceSupplier(() -> new MicronautPostProcess(context)); registry.registerBeanDefinition("micronautPostProcess", ppd); + genericBeanDefinition.setBeanClass(context.getClass()); genericBeanDefinition.setInstanceSupplier(() -> context); genericBeanDefinition.setDestroyMethodName("stop"); registry.registerBeanDefinition( diff --git a/spring-boot-starter/src/test/java/io/micronaut/spring/boot/starter/EnableMicronautTest.java b/spring-boot-starter/src/test/java/io/micronaut/spring/boot/starter/EnableMicronautTest.java index 4237ea27d..e2a4f7d45 100644 --- a/spring-boot-starter/src/test/java/io/micronaut/spring/boot/starter/EnableMicronautTest.java +++ b/spring-boot-starter/src/test/java/io/micronaut/spring/boot/starter/EnableMicronautTest.java @@ -1,6 +1,8 @@ package io.micronaut.spring.boot.starter; +import java.util.Arrays; import java.util.List; +import java.util.Objects; import io.micronaut.context.ApplicationContext; import io.micronaut.context.annotation.Bean; @@ -15,6 +17,7 @@ import org.springframework.core.env.Environment; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -51,6 +54,12 @@ void testEnableMicronaut() { assertEquals("default", fooBean.getConfiguration().getName()); assertEquals(3, fooBeanList.size()); assertTrue(context.isRunning()); + assertFalse( + context.getAllBeanDefinitions() + .stream() + .map(bean -> bean.getBeanType()) + .anyMatch(Objects::isNull) + ); } }