diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 4c71d9ad83c..f59e60ea30c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -25,7 +25,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -46,6 +45,7 @@ import org.springframework.aot.generate.GeneratedMethod; import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.hint.ExecutableMode; import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.support.ClassHintUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; @@ -1018,10 +1018,10 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescriptor dependencyDescriptor) { if (this.candidateResolver != null) { - Class proxyType = + Class proxyClass = this.candidateResolver.getLazyResolutionProxyClass(dependencyDescriptor, null); - if (proxyType != null && Proxy.isProxyClass(proxyType)) { - runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces()); + if (proxyClass != null) { + ClassHintUtils.registerProxyIfNecessary(proxyClass, runtimeHints); } } } diff --git a/spring-context/src/main/java/org/springframework/context/aot/CglibClassHandler.java b/spring-context/src/main/java/org/springframework/context/aot/CglibClassHandler.java index 4401342fd81..61b393f801f 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/CglibClassHandler.java +++ b/spring-context/src/main/java/org/springframework/context/aot/CglibClassHandler.java @@ -16,7 +16,6 @@ package org.springframework.context.aot; -import java.util.function.BiConsumer; import java.util.function.Consumer; import org.springframework.aot.generate.GeneratedFiles; @@ -26,6 +25,7 @@ import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.TypeHint.Builder; import org.springframework.aot.hint.TypeReference; +import org.springframework.aot.hint.support.ClassHintUtils; import org.springframework.cglib.core.ReflectUtils; import org.springframework.core.io.ByteArrayResource; @@ -34,8 +34,10 @@ import org.springframework.core.io.ByteArrayResource; * and register the necessary hints so that they can be instantiated. * * @author Stephane Nicoll - * @see ReflectUtils#setGeneratedClassHandler(BiConsumer) - * @see ReflectUtils#setLoadedClassHandler(Consumer) + * @since 6.0 + * @see ReflectUtils#setGeneratedClassHandler + * @see ReflectUtils#setLoadedClassHandler + * @see ClassHintUtils#registerProxyIfNecessary */ class CglibClassHandler { @@ -46,11 +48,13 @@ class CglibClassHandler { private final GeneratedFiles generatedFiles; + CglibClassHandler(GenerationContext generationContext) { this.runtimeHints = generationContext.getRuntimeHints(); this.generatedFiles = generationContext.getGeneratedFiles(); } + /** * Handle the specified generated CGLIB class. * @param cglibClassName the name of the generated class diff --git a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java index 2efd033d599..c172acc02d8 100644 --- a/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java @@ -18,15 +18,12 @@ package org.springframework.context.support; import java.io.IOException; import java.lang.reflect.Constructor; -import java.lang.reflect.Proxy; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Consumer; import java.util.function.Supplier; -import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; -import org.springframework.aot.hint.TypeHint.Builder; +import org.springframework.aot.hint.support.ClassHintUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanDefinitionStoreException; @@ -49,7 +46,6 @@ import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.metrics.ApplicationStartup; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; /** * Generic ApplicationContext implementation that holds a single internal @@ -72,7 +68,7 @@ import org.springframework.util.ClassUtils; * definitions on it. {@link #refresh()} may only be called once. * *

This ApplicationContext implementation is suitable for Ahead of Time - * processing, using {@link #refreshForAotProcessing()} as an alternative to the + * processing, using {@link #refreshForAotProcessing} as an alternative to the * regular {@link #refresh()}. * *

Usage example: @@ -88,16 +84,13 @@ import org.springframework.util.ClassUtils; * MyBean myBean = (MyBean) ctx.getBean("myBean"); * ... * - * For the typical case of XML bean definitions, simply use + * For the typical case of XML bean definitions, you may also use * {@link ClassPathXmlApplicationContext} or {@link FileSystemXmlApplicationContext}, * which are easier to set up - but less flexible, since you can just use standard * resource locations for XML bean definitions, rather than mixing arbitrary bean - * definition formats. The equivalent in a web environment is - * {@link org.springframework.web.context.support.XmlWebApplicationContext}. - * - *

For custom application context implementations that are supposed to read - * special bean definition formats in a refreshable manner, consider deriving - * from the {@link AbstractRefreshableApplicationContext} base class. + * definition formats. For a custom application context implementation supposed to + * read a specific bean definition format in a refreshable manner, consider + * deriving from the {@link AbstractRefreshableApplicationContext} base class. * * @author Juergen Hoeller * @author Chris Beams @@ -111,15 +104,6 @@ import org.springframework.util.ClassUtils; */ public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry { - private static final Consumer asClassBasedProxy = hint -> - hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, - MemberCategory.INVOKE_DECLARED_METHODS, - MemberCategory.DECLARED_FIELDS); - - private static final Consumer asProxiedUserClass = hint -> - hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS); - - private final DefaultListableBeanFactory beanFactory; @Nullable @@ -442,11 +426,11 @@ public class GenericApplicationContext extends AbstractApplicationContext implem for (String beanName : this.beanFactory.getBeanDefinitionNames()) { Class beanType = this.beanFactory.getType(beanName); if (beanType != null) { - registerProxyHintIfNecessary(beanType, runtimeHints); + ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints); for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) { Class newBeanType = bpp.determineBeanType(beanType, beanName); if (newBeanType != beanType) { - registerProxyHintIfNecessary(newBeanType, runtimeHints); + ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints); beanType = newBeanType; } } @@ -454,22 +438,6 @@ public class GenericApplicationContext extends AbstractApplicationContext implem } } - private void registerProxyHintIfNecessary(Class beanType, RuntimeHints runtimeHints) { - if (Proxy.isProxyClass(beanType)) { - // A JDK proxy class needs an explicit hint - runtimeHints.proxies().registerJdkProxy(beanType.getInterfaces()); - } - else { - // Potentially a CGLIB-generated subclass with reflection hints - Class userClass = ClassUtils.getUserClass(beanType); - if (userClass != beanType) { - runtimeHints.reflection() - .registerType(beanType, asClassBasedProxy) - .registerType(userClass, asProxiedUserClass); - } - } - } - //--------------------------------------------------------------------- // Convenient methods for registering individual beans diff --git a/spring-core/src/main/java/org/springframework/aot/hint/support/ClassHintUtils.java b/spring-core/src/main/java/org/springframework/aot/hint/support/ClassHintUtils.java new file mode 100644 index 00000000000..e38fdaaa970 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/aot/hint/support/ClassHintUtils.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.aot.hint.support; + +import java.lang.reflect.Proxy; +import java.util.function.Consumer; + +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.TypeHint; +import org.springframework.util.ClassUtils; + +/** + * Utilities for core hint inference on Spring-managed classes, + * specifically for proxy types such as interface-based JDK proxies + * and CGLIB-generated subclasses which need proxy/reflection hints. + * + *

Note that this class does not take specifics of Spring AOP or + * any other framework arrangement into account. It just operates + * on the JDK and CGLIB proxy facilities and their core conventions. + * + * @author Juergen Hoeller + * @since 6.0.3 + * @see org.springframework.aot.hint.ProxyHints + * @see org.springframework.aot.hint.ReflectionHints + */ +public abstract class ClassHintUtils { + + private static final Consumer asClassBasedProxy = hint -> + hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, + MemberCategory.INVOKE_DECLARED_METHODS, + MemberCategory.DECLARED_FIELDS); + + private static final Consumer asProxiedUserClass = hint -> + hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS); + + + /** + * Register a proxy hint for a JDK proxy or corresponding reflection hints + * for a CGLIB-generated subclass, if necessary. + * @param candidateClass the class to introspect + * @param runtimeHints the RuntimeHints instance to register the hints on + * @see Proxy#isProxyClass(Class) + * @see ClassUtils#getUserClass(Class) + */ + public static void registerProxyIfNecessary(Class candidateClass, RuntimeHints runtimeHints) { + if (Proxy.isProxyClass(candidateClass)) { + // A JDK proxy class needs an explicit hint + runtimeHints.proxies().registerJdkProxy(candidateClass.getInterfaces()); + } + else { + // Potentially a CGLIB-generated subclass with reflection hints + Class userClass = ClassUtils.getUserClass(candidateClass); + if (userClass != candidateClass) { + runtimeHints.reflection() + .registerType(candidateClass, asClassBasedProxy) + .registerType(userClass, asProxiedUserClass); + } + } + } + +}