From 57884a7c4127e707bb3f0dee7bc9107d51bca6d0 Mon Sep 17 00:00:00 2001 From: Marcus Hert Da Coregio Date: Wed, 3 Apr 2024 14:05:08 -0300 Subject: [PATCH] Saving --- ...utowireBeanFactoryObjectPostProcessor.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java b/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java index 7792ff44ce4..cb8b9c4a440 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java +++ b/config/src/main/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessor.java @@ -21,12 +21,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.NotNull; +import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.Aware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.core.NativeDetector; import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.util.Assert; @@ -55,14 +59,13 @@ final class AutowireBeanFactoryObjectPostProcessor } @Override - @SuppressWarnings("unchecked") public T postProcess(T object) { if (object == null) { return null; } T result = null; try { - result = (T) this.autowireBeanFactory.initializeBean(object, object.toString()); + result = initializeBeanIfNeeded(object); } catch (RuntimeException ex) { Class type = object.getClass(); @@ -78,6 +81,23 @@ public T postProcess(T object) { return result; } + @SuppressWarnings("unchecked") + private T initializeBeanIfNeeded(T object) { + if (!NativeDetector.inNativeImage() || !AopUtils.isCglibProxy(object)) { + return (T) this.autowireBeanFactory.initializeBean(object, object.toString()); + } + ObjectProvider provider = this.autowireBeanFactory.getBeanProvider(object.getClass()); + Object bean = provider.getIfUnique(); + if (bean == null) { + String msg = """ + Failed to resolve an unique bean (single or primary) of type [%s] from the BeanFactory. + Because the object is a CGLIB Proxy, a raw bean cannot be initialized during runtime in a native image. + """.formatted(object.getClass()); + throw new IllegalStateException(msg); + } + return (T) bean; + } + @Override public void afterSingletonsInstantiated() { for (SmartInitializingSingleton singleton : this.smartSingletons) {