From 86e75c54a6bdbfe0bbae387c83a5c812770adf60 Mon Sep 17 00:00:00 2001 From: fenghui Date: Fri, 26 Jun 2026 11:34:08 +0800 Subject: [PATCH] fix: resolve BeanPostProcessors auto-proxy warning (#257) --- .../springboot/ForestAutoConfiguration.java | 23 +++----- .../forest/springboot/ForestBeanRegister.java | 25 +++++---- .../springboot/ForestAutoConfiguration.java | 19 +++---- .../forest/springboot/ForestBeanRegister.java | 25 +++++---- .../test/ForestAutoProxyWarningTest.java | 55 +++++++++++++++++++ 5 files changed, 95 insertions(+), 52 deletions(-) create mode 100644 forest-test/forest-spring-boot3-test/src/test/java/com/dtflys/forest/springboot3/test/ForestAutoProxyWarningTest.java diff --git a/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java b/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java index 6cce1f62..0c1ec510 100644 --- a/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java +++ b/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java @@ -13,21 +13,15 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Role; -import javax.annotation.Resource; - @Configuration @EnableConfigurationProperties({ForestConfigurationProperties.class}) @Import({ForestScannerRegister.class}) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class ForestAutoConfiguration { - @Resource - private ConfigurableApplicationContext applicationContext; - @Bean @ConditionalOnMissingBean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) @@ -51,28 +45,25 @@ public SpringInterceptorFactory forestInterceptorFactory() { @Bean - @DependsOn("forestBeanProcessor") @ConditionalOnMissingBean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) - public ForestBeanRegister forestBeanRegister(SpringForestProperties properties, - SpringForestObjectFactory forestObjectFactory, - SpringInterceptorFactory forestInterceptorFactory, - ForestConfigurationProperties forestConfigurationProperties) { - ForestBeanRegister register = new ForestBeanRegister( + public static ForestBeanRegister forestBeanRegister(ConfigurableApplicationContext applicationContext, + SpringForestProperties properties, + SpringForestObjectFactory forestObjectFactory, + SpringInterceptorFactory forestInterceptorFactory, + ForestConfigurationProperties forestConfigurationProperties) { + return new ForestBeanRegister( applicationContext, forestConfigurationProperties, properties, forestObjectFactory, forestInterceptorFactory); - register.registerForestConfiguration(); - register.registerScanner(); - return register; } @Bean @ConditionalOnMissingBean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) - public ForestBeanProcessor forestBeanProcessor() { + public static ForestBeanProcessor forestBeanProcessor() { return new ForestBeanProcessor(); } diff --git a/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java b/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java index 7fe81505..4b82a124 100644 --- a/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java +++ b/forest-spring-boot-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java @@ -18,11 +18,11 @@ import com.dtflys.forest.springboot.properties.ForestSSLKeyStoreProperties; import com.dtflys.forest.utils.ForestDataType; import com.dtflys.forest.utils.StringUtils; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.ManagedMap; import org.springframework.cglib.core.ReflectUtils; import org.springframework.context.ConfigurableApplicationContext; @@ -39,7 +39,7 @@ import java.util.Map; import java.util.stream.Collectors; -public class ForestBeanRegister implements ResourceLoaderAware, BeanPostProcessor { +public class ForestBeanRegister implements ResourceLoaderAware, BeanDefinitionRegistryPostProcessor { private final ConfigurableApplicationContext applicationContext; @@ -71,6 +71,16 @@ public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { + registerForestConfiguration(); + registerScanner(); + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { + } + public ForestConfiguration registerForestConfiguration() { BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ForestConfiguration.class); @@ -296,13 +306,4 @@ public ClassPathClientScanner registerScanner() { return scanner; } - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - return bean; - } } diff --git a/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java b/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java index 433a2557..1f07b660 100644 --- a/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java +++ b/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestAutoConfiguration.java @@ -12,7 +12,6 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Role; @@ -46,29 +45,25 @@ public SpringInterceptorFactory forestInterceptorFactory() { @Bean - @DependsOn("forestBeanProcessor") @ConditionalOnMissingBean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) - public ForestBeanRegister forestBeanRegister(ConfigurableApplicationContext applicationContext, - SpringForestProperties properties, - SpringForestObjectFactory forestObjectFactory, - SpringInterceptorFactory forestInterceptorFactory, - ForestConfigurationProperties forestConfigurationProperties) { - ForestBeanRegister register = new ForestBeanRegister( + public static ForestBeanRegister forestBeanRegister(ConfigurableApplicationContext applicationContext, + SpringForestProperties properties, + SpringForestObjectFactory forestObjectFactory, + SpringInterceptorFactory forestInterceptorFactory, + ForestConfigurationProperties forestConfigurationProperties) { + return new ForestBeanRegister( applicationContext, forestConfigurationProperties, properties, forestObjectFactory, forestInterceptorFactory); - register.registerForestConfiguration(); - register.registerScanner(); - return register; } @Bean @ConditionalOnMissingBean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) - public ForestBeanProcessor forestBeanProcessor() { + public static ForestBeanProcessor forestBeanProcessor() { return new ForestBeanProcessor(); } diff --git a/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java b/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java index 9d9b3cfd..7f0ffee3 100644 --- a/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java +++ b/forest-spring-boot3-starter/src/main/java/com/dtflys/forest/springboot/ForestBeanRegister.java @@ -18,9 +18,9 @@ import com.dtflys.forest.springboot.properties.ForestSSLKeyStoreProperties; import com.dtflys.forest.utils.ForestDataType; import com.dtflys.forest.utils.StringUtils; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.ManagedMap; @@ -38,7 +38,7 @@ import java.util.List; import java.util.Map; -public class ForestBeanRegister implements ResourceLoaderAware, BeanPostProcessor { +public class ForestBeanRegister implements ResourceLoaderAware, BeanDefinitionRegistryPostProcessor { private final ConfigurableApplicationContext applicationContext; @@ -70,6 +70,16 @@ public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { + registerForestConfiguration(); + registerScanner(); + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { + } + public ForestConfiguration registerForestConfiguration() { BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ForestConfiguration.class); @@ -294,13 +304,4 @@ public ClassPathClientScanner registerScanner() { return scanner; } - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - return bean; - } } diff --git a/forest-test/forest-spring-boot3-test/src/test/java/com/dtflys/forest/springboot3/test/ForestAutoProxyWarningTest.java b/forest-test/forest-spring-boot3-test/src/test/java/com/dtflys/forest/springboot3/test/ForestAutoProxyWarningTest.java new file mode 100644 index 00000000..63cadbff --- /dev/null +++ b/forest-test/forest-spring-boot3-test/src/test/java/com/dtflys/forest/springboot3/test/ForestAutoProxyWarningTest.java @@ -0,0 +1,55 @@ +package com.dtflys.forest.springboot3.test; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import com.dtflys.forest.springboot.annotation.ForestScannerRegister; +import org.junit.Test; +import org.slf4j.LoggerFactory; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.AutoConfigurationPackage; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.event.ApplicationFailedEvent; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; + +import static org.junit.Assert.assertFalse; + +public class ForestAutoProxyWarningTest extends BaseSpringBootTest { + + @Test + public void testNoBeanPostProcessorCheckerWarning() { + Logger logger = (Logger) LoggerFactory.getLogger( + "org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker"); + ListAppender appender = new ListAppender<>(); + appender.start(); + logger.addAppender(appender); + Level level = logger.getLevel(); + logger.setLevel(Level.INFO); + + try (ConfigurableApplicationContext ignored = new SpringApplicationBuilder(TestApplication.class) + .web(WebApplicationType.NONE) + .listeners(event -> { + if (event instanceof ApplicationFailedEvent) { + ForestScannerRegister.cleanBackPackages(); + } + }) + .run("--spring.main.banner-mode=off")) { + assertFalse(appender.list.stream() + .map(ILoggingEvent::getFormattedMessage) + .anyMatch(message -> message.contains("not eligible for getting processed by all BeanPostProcessors") + && message.contains("forestBeanProcessor"))); + } finally { + logger.detachAppender(appender); + logger.setLevel(level); + } + } + + @Configuration + @EnableAutoConfiguration + @AutoConfigurationPackage + static class TestApplication { + } +}