diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java index 0a1f0a7d69..e7eafe4158 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * 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. @@ -100,6 +100,20 @@ public interface AutowireCandidateResolver { return null; } + /** + * Determine the proxy class for lazy resolution of the dependency target, + * if demanded by the injection point. + *

The default implementation simply returns {@code null}. + * @param descriptor the descriptor for the target method parameter or field + * @param beanName the name of the bean that contains the injection point + * @return the lazy resolution proxy class for the dependency target, if any + * @since 6.0 + */ + @Nullable + default Class getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) { + return null; + } + /** * Return a clone of this resolver instance if necessary, retaining its local * configuration and allowing for the cloned instance to get associated with diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java index f33eeecf16..2afdf73924 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * 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. @@ -64,6 +64,12 @@ public class SimpleAutowireCandidateResolver implements AutowireCandidateResolve return null; } + @Override + @Nullable + public Class getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) { + return null; + } + /** * This implementation returns {@code this} as-is. * @see #INSTANCE diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java index 93999ddc8c..f50fdff6a1 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * 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. @@ -54,6 +54,12 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat return (isLazy(descriptor) ? buildLazyResolutionProxy(descriptor, beanName) : null); } + @Override + @Nullable + public Class getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) { + return (isLazy(descriptor) ? (Class) buildLazyResolutionProxy(descriptor, beanName, true) : null); + } + protected boolean isLazy(DependencyDescriptor descriptor) { for (Annotation ann : descriptor.getAnnotations()) { Lazy lazy = AnnotationUtils.getAnnotation(ann, Lazy.class); @@ -74,7 +80,13 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat return false; } - protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final @Nullable String beanName) { + protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, @Nullable String beanName) { + return buildLazyResolutionProxy(descriptor, beanName, false); + } + + private Object buildLazyResolutionProxy( + final DependencyDescriptor descriptor, final @Nullable String beanName, boolean classOnly) { + BeanFactory beanFactory = getBeanFactory(); Assert.state(beanFactory instanceof DefaultListableBeanFactory, "BeanFactory needs to be a DefaultListableBeanFactory"); @@ -127,7 +139,8 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat if (dependencyType.isInterface()) { pf.addInterface(dependencyType); } - return pf.getProxy(dlbf.getBeanClassLoader()); + ClassLoader classLoader = dlbf.getBeanClassLoader(); + return (classOnly ? pf.getProxyClass(classLoader) : pf.getProxy(classLoader)); } }