@@ -991,54 +991,60 @@ protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd,
991
991
*/
992
992
@ Nullable
993
993
private FactoryBean <?> getSingletonFactoryBeanForTypeCheck (String beanName , RootBeanDefinition mbd ) {
994
- BeanWrapper bw = this .factoryBeanInstanceCache .get (beanName );
995
- if (bw != null ) {
996
- return (FactoryBean <?>) bw .getWrappedInstance ();
997
- }
998
- Object beanInstance = getSingleton (beanName , false );
999
- if (beanInstance instanceof FactoryBean <?> factoryBean ) {
1000
- return factoryBean ;
1001
- }
1002
- if (isSingletonCurrentlyInCreation (beanName ) ||
1003
- (mbd .getFactoryBeanName () != null && isSingletonCurrentlyInCreation (mbd .getFactoryBeanName ()))) {
1004
- return null ;
1005
- }
1006
-
1007
- Object instance ;
994
+ this .singletonLock .lock ();
1008
995
try {
1009
- // Mark this bean as currently in creation, even if just partially.
1010
- beforeSingletonCreation (beanName );
1011
- // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
1012
- instance = resolveBeforeInstantiation (beanName , mbd );
1013
- if (instance == null ) {
1014
- bw = createBeanInstance (beanName , mbd , null );
1015
- instance = bw .getWrappedInstance ();
1016
- this .factoryBeanInstanceCache .put (beanName , bw );
996
+ BeanWrapper bw = this .factoryBeanInstanceCache .get (beanName );
997
+ if (bw != null ) {
998
+ return (FactoryBean <?>) bw .getWrappedInstance ();
1017
999
}
1018
- }
1019
- catch (UnsatisfiedDependencyException ex ) {
1020
- // Don't swallow, probably misconfiguration...
1021
- throw ex ;
1022
- }
1023
- catch (BeanCreationException ex ) {
1024
- // Don't swallow a linkage error since it contains a full stacktrace on
1025
- // first occurrence... and just a plain NoClassDefFoundError afterwards.
1026
- if (ex .contains (LinkageError .class )) {
1000
+ Object beanInstance = getSingleton (beanName , false );
1001
+ if (beanInstance instanceof FactoryBean <?> factoryBean ) {
1002
+ return factoryBean ;
1003
+ }
1004
+ if (isSingletonCurrentlyInCreation (beanName ) ||
1005
+ (mbd .getFactoryBeanName () != null && isSingletonCurrentlyInCreation (mbd .getFactoryBeanName ()))) {
1006
+ return null ;
1007
+ }
1008
+
1009
+ Object instance ;
1010
+ try {
1011
+ // Mark this bean as currently in creation, even if just partially.
1012
+ beforeSingletonCreation (beanName );
1013
+ // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
1014
+ instance = resolveBeforeInstantiation (beanName , mbd );
1015
+ if (instance == null ) {
1016
+ bw = createBeanInstance (beanName , mbd , null );
1017
+ instance = bw .getWrappedInstance ();
1018
+ this .factoryBeanInstanceCache .put (beanName , bw );
1019
+ }
1020
+ }
1021
+ catch (UnsatisfiedDependencyException ex ) {
1022
+ // Don't swallow, probably misconfiguration...
1027
1023
throw ex ;
1028
1024
}
1029
- // Instantiation failure, maybe too early...
1030
- if (logger .isDebugEnabled ()) {
1031
- logger .debug ("Bean creation exception on singleton FactoryBean type check: " + ex );
1025
+ catch (BeanCreationException ex ) {
1026
+ // Don't swallow a linkage error since it contains a full stacktrace on
1027
+ // first occurrence... and just a plain NoClassDefFoundError afterwards.
1028
+ if (ex .contains (LinkageError .class )) {
1029
+ throw ex ;
1030
+ }
1031
+ // Instantiation failure, maybe too early...
1032
+ if (logger .isDebugEnabled ()) {
1033
+ logger .debug ("Bean creation exception on singleton FactoryBean type check: " + ex );
1034
+ }
1035
+ onSuppressedException (ex );
1036
+ return null ;
1032
1037
}
1033
- onSuppressedException (ex );
1034
- return null ;
1038
+ finally {
1039
+ // Finished partial creation of this bean.
1040
+ afterSingletonCreation (beanName );
1041
+ }
1042
+
1043
+ return getFactoryBean (beanName , instance );
1035
1044
}
1036
1045
finally {
1037
- // Finished partial creation of this bean.
1038
- afterSingletonCreation (beanName );
1046
+ this .singletonLock .unlock ();
1039
1047
}
1040
-
1041
- return getFactoryBean (beanName , instance );
1042
1048
}
1043
1049
1044
1050
/**
0 commit comments