Skip to content

Commit 4eb84a0

Browse files
committed
Introduce getLazyResolutionProxyClass mechanism in AutowireCandidateResolver SPI
See gh-28980
1 parent 8a51e31 commit 4eb84a0

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,6 +100,20 @@ default Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor
100100
return null;
101101
}
102102

103+
/**
104+
* Determine the proxy class for lazy resolution of the dependency target,
105+
* if demanded by the injection point.
106+
* <p>The default implementation simply returns {@code null}.
107+
* @param descriptor the descriptor for the target method parameter or field
108+
* @param beanName the name of the bean that contains the injection point
109+
* @return the lazy resolution proxy class for the dependency target, if any
110+
* @since 6.0
111+
*/
112+
@Nullable
113+
default Class<?> getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) {
114+
return null;
115+
}
116+
103117
/**
104118
* Return a clone of this resolver instance if necessary, retaining its local
105119
* configuration and allowing for the cloned instance to get associated with

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,6 +64,12 @@ public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor,
6464
return null;
6565
}
6666

67+
@Override
68+
@Nullable
69+
public Class<?> getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) {
70+
return null;
71+
}
72+
6773
/**
6874
* This implementation returns {@code this} as-is.
6975
* @see #INSTANCE

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,12 @@ public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor,
5454
return (isLazy(descriptor) ? buildLazyResolutionProxy(descriptor, beanName) : null);
5555
}
5656

57+
@Override
58+
@Nullable
59+
public Class<?> getLazyResolutionProxyClass(DependencyDescriptor descriptor, @Nullable String beanName) {
60+
return (isLazy(descriptor) ? (Class<?>) buildLazyResolutionProxy(descriptor, beanName, true) : null);
61+
}
62+
5763
protected boolean isLazy(DependencyDescriptor descriptor) {
5864
for (Annotation ann : descriptor.getAnnotations()) {
5965
Lazy lazy = AnnotationUtils.getAnnotation(ann, Lazy.class);
@@ -74,7 +80,13 @@ protected boolean isLazy(DependencyDescriptor descriptor) {
7480
return false;
7581
}
7682

77-
protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final @Nullable String beanName) {
83+
protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, @Nullable String beanName) {
84+
return buildLazyResolutionProxy(descriptor, beanName, false);
85+
}
86+
87+
private Object buildLazyResolutionProxy(
88+
final DependencyDescriptor descriptor, final @Nullable String beanName, boolean classOnly) {
89+
7890
BeanFactory beanFactory = getBeanFactory();
7991
Assert.state(beanFactory instanceof DefaultListableBeanFactory,
8092
"BeanFactory needs to be a DefaultListableBeanFactory");
@@ -127,7 +139,8 @@ public void releaseTarget(Object target) {
127139
if (dependencyType.isInterface()) {
128140
pf.addInterface(dependencyType);
129141
}
130-
return pf.getProxy(dlbf.getBeanClassLoader());
142+
ClassLoader classLoader = dlbf.getBeanClassLoader();
143+
return (classOnly ? pf.getProxyClass(classLoader) : pf.getProxy(classLoader));
131144
}
132145

133146
}

0 commit comments

Comments
 (0)