Browse Source

Introduce getLazyResolutionProxyClass mechanism in AutowireCandidateResolver SPI

See gh-28980
pull/28958/head
Juergen Hoeller 3 years ago
parent
commit
4eb84a0537
  1. 16
      spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java
  2. 8
      spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java
  3. 19
      spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java

16
spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireCandidateResolver.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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.
* <p>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

8
spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleAutowireCandidateResolver.java

@ -1,5 +1,5 @@ @@ -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 @@ -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

19
spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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 @@ -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));
}
}

Loading…
Cancel
Save