Browse Source

Polishing

5.0.x
Juergen Hoeller 6 years ago
parent
commit
3db0bd2309
  1. 28
      spring-beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java
  2. 52
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
  3. 30
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  4. 36
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  5. 12
      spring-beans/src/main/java/org/springframework/beans/factory/support/FactoryBeanRegistrySupport.java

28
spring-beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,7 +70,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
private CachedIntrospectionResults cachedIntrospectionResults; private CachedIntrospectionResults cachedIntrospectionResults;
/** /**
* The security context used for invoking the property methods * The security context used for invoking the property methods.
*/ */
@Nullable @Nullable
private AccessControlContext acc; private AccessControlContext acc;
@ -97,7 +97,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
/** /**
* Create a new BeanWrapperImpl for the given object. * Create a new BeanWrapperImpl for the given object.
* @param object object wrapped by this BeanWrapper * @param object the object wrapped by this BeanWrapper
*/ */
public BeanWrapperImpl(Object object) { public BeanWrapperImpl(Object object) {
super(object); super(object);
@ -114,7 +114,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
/** /**
* Create a new BeanWrapperImpl for the given object, * Create a new BeanWrapperImpl for the given object,
* registering a nested path that the object is in. * registering a nested path that the object is in.
* @param object object wrapped by this BeanWrapper * @param object the object wrapped by this BeanWrapper
* @param nestedPath the nested path of the object * @param nestedPath the nested path of the object
* @param rootObject the root object at the top of the path * @param rootObject the root object at the top of the path
*/ */
@ -125,7 +125,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
/** /**
* Create a new BeanWrapperImpl for the given object, * Create a new BeanWrapperImpl for the given object,
* registering a nested path that the object is in. * registering a nested path that the object is in.
* @param object object wrapped by this BeanWrapper * @param object the object wrapped by this BeanWrapper
* @param nestedPath the nested path of the object * @param nestedPath the nested path of the object
* @param parent the containing BeanWrapper (must not be {@code null}) * @param parent the containing BeanWrapper (must not be {@code null})
*/ */
@ -166,7 +166,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
} }
/** /**
* Obtain a lazily initializted CachedIntrospectionResults instance * Obtain a lazily initialized CachedIntrospectionResults instance
* for the wrapped object. * for the wrapped object.
*/ */
private CachedIntrospectionResults getCachedIntrospectionResults() { private CachedIntrospectionResults getCachedIntrospectionResults() {
@ -283,21 +283,21 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
@Override @Override
@Nullable @Nullable
public TypeDescriptor nested(int level) { public TypeDescriptor nested(int level) {
return TypeDescriptor.nested(property(pd), level); return TypeDescriptor.nested(property(this.pd), level);
} }
@Override @Override
@Nullable @Nullable
public Object getValue() throws Exception { public Object getValue() throws Exception {
final Method readMethod = this.pd.getReadMethod(); Method readMethod = this.pd.getReadMethod();
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
ReflectionUtils.makeAccessible(readMethod); ReflectionUtils.makeAccessible(readMethod);
return null; return null;
}); });
try { try {
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> return AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
readMethod.invoke(getWrappedInstance(), (Object[]) null), acc); () -> readMethod.invoke(getWrappedInstance(), (Object[]) null), acc);
} }
catch (PrivilegedActionException pae) { catch (PrivilegedActionException pae) {
throw pae.getException(); throw pae.getException();
@ -310,8 +310,8 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
} }
@Override @Override
public void setValue(final @Nullable Object value) throws Exception { public void setValue(@Nullable Object value) throws Exception {
final Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ? Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() : ((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() :
this.pd.getWriteMethod()); this.pd.getWriteMethod());
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
@ -320,8 +320,8 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
return null; return null;
}); });
try { try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
writeMethod.invoke(getWrappedInstance(), value), acc); () -> writeMethod.invoke(getWrappedInstance(), value), acc);
} }
catch (PrivilegedActionException ex) { catch (PrivilegedActionException ex) {
throw ex.getException(); throw ex.getException();

52
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -305,7 +305,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
public void autowireBean(Object existingBean) { public void autowireBean(Object existingBean) {
// Use non-singleton bean definition, to avoid registering bean as dependent bean. // Use non-singleton bean definition, to avoid registering bean as dependent bean.
RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean)); RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean));
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setScope(SCOPE_PROTOTYPE);
bd.allowCaching = ClassUtils.isCacheSafe(bd.getBeanClass(), getBeanClassLoader()); bd.allowCaching = ClassUtils.isCacheSafe(bd.getBeanClass(), getBeanClassLoader());
BeanWrapper bw = new BeanWrapperImpl(existingBean); BeanWrapper bw = new BeanWrapperImpl(existingBean);
initBeanWrapper(bw); initBeanWrapper(bw);
@ -325,7 +325,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
bd = new RootBeanDefinition(mbd); bd = new RootBeanDefinition(mbd);
} }
if (!bd.isPrototype()) { if (!bd.isPrototype()) {
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setScope(SCOPE_PROTOTYPE);
bd.allowCaching = ClassUtils.isCacheSafe(ClassUtils.getUserClass(existingBean), getBeanClassLoader()); bd.allowCaching = ClassUtils.isCacheSafe(ClassUtils.getUserClass(existingBean), getBeanClassLoader());
} }
BeanWrapper bw = new BeanWrapperImpl(existingBean); BeanWrapper bw = new BeanWrapperImpl(existingBean);
@ -349,28 +349,27 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
public Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException { public Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
// Use non-singleton bean definition, to avoid registering bean as dependent bean. // Use non-singleton bean definition, to avoid registering bean as dependent bean.
RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck); RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setScope(SCOPE_PROTOTYPE);
return createBean(beanClass.getName(), bd, null); return createBean(beanClass.getName(), bd, null);
} }
@Override @Override
public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException { public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
// Use non-singleton bean definition, to avoid registering bean as dependent bean. // Use non-singleton bean definition, to avoid registering bean as dependent bean.
final RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck); RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setScope(SCOPE_PROTOTYPE);
if (bd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR) { if (bd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR) {
return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance(); return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance();
} }
else { else {
Object bean; Object bean;
final BeanFactory parent = this;
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
bean = AccessController.doPrivileged((PrivilegedAction<Object>) () -> bean = AccessController.doPrivileged(
getInstantiationStrategy().instantiate(bd, null, parent), (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(bd, null, this),
getAccessControlContext()); getAccessControlContext());
} }
else { else {
bean = getInstantiationStrategy().instantiate(bd, null, parent); bean = getInstantiationStrategy().instantiate(bd, null, this);
} }
populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean)); populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean));
return bean; return bean;
@ -387,7 +386,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// Use non-singleton bean definition, to avoid registering bean as dependent bean. // Use non-singleton bean definition, to avoid registering bean as dependent bean.
RootBeanDefinition bd = RootBeanDefinition bd =
new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck); new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setScope(SCOPE_PROTOTYPE);
BeanWrapper bw = new BeanWrapperImpl(existingBean); BeanWrapper bw = new BeanWrapperImpl(existingBean);
initBeanWrapper(bw); initBeanWrapper(bw);
populateBean(bd.getBeanClass().getName(), bd, bw); populateBean(bd.getBeanClass().getName(), bd, bw);
@ -523,7 +522,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @see #instantiateUsingFactoryMethod * @see #instantiateUsingFactoryMethod
* @see #autowireConstructor * @see #autowireConstructor
*/ */
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException { throws BeanCreationException {
// Instantiate the bean. // Instantiate the bean.
@ -534,7 +533,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (instanceWrapper == null) { if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args); instanceWrapper = createBeanInstance(beanName, mbd, args);
} }
final Object bean = instanceWrapper.getWrappedInstance(); Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass(); Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) { if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType; mbd.resolvedTargetType = beanType;
@ -603,7 +602,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
"] in its raw version as part of a circular reference, but has eventually been " + "] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " + "wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " + "bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
} }
} }
} }
@ -860,7 +859,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @return the common {@code FactoryBean} object type, or {@code null} if none * @return the common {@code FactoryBean} object type, or {@code null} if none
*/ */
@Nullable @Nullable
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, final String factoryMethodName) { private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, String factoryMethodName) {
class Holder { @Nullable Class<?> value = null; } class Holder { @Nullable Class<?> value = null; }
final Holder objectType = new Holder(); final Holder objectType = new Holder();
@ -1206,17 +1205,16 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @param mbd the bean definition for the bean * @param mbd the bean definition for the bean
* @return a BeanWrapper for the new instance * @return a BeanWrapper for the new instance
*/ */
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
try { try {
Object beanInstance; Object beanInstance;
final BeanFactory parent = this;
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () -> beanInstance = AccessController.doPrivileged(
getInstantiationStrategy().instantiate(mbd, beanName, parent), (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
getAccessControlContext()); getAccessControlContext());
} }
else { else {
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent); beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
} }
BeanWrapper bw = new BeanWrapperImpl(beanInstance); BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw); initBeanWrapper(bw);
@ -1663,7 +1661,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @see #invokeInitMethods * @see #invokeInitMethods
* @see #applyBeanPostProcessorsAfterInitialization * @see #applyBeanPostProcessorsAfterInitialization
*/ */
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean); invokeAwareMethods(beanName, bean);
@ -1694,7 +1692,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return wrappedBean; return wrappedBean;
} }
private void invokeAwareMethods(final String beanName, final Object bean) { private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) { if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) { if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName); ((BeanNameAware) bean).setBeanName(beanName);
@ -1723,7 +1721,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @throws Throwable if thrown by init methods or by the invocation process * @throws Throwable if thrown by init methods or by the invocation process
* @see #invokeCustomInitMethod * @see #invokeCustomInitMethod
*/ */
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd) protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable { throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean); boolean isInitializingBean = (bean instanceof InitializingBean);
@ -1764,12 +1762,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* methods with arguments. * methods with arguments.
* @see #invokeInitMethods * @see #invokeInitMethods
*/ */
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefinition mbd)
throws Throwable { throws Throwable {
String initMethodName = mbd.getInitMethodName(); String initMethodName = mbd.getInitMethodName();
Assert.state(initMethodName != null, "No init method set"); Assert.state(initMethodName != null, "No init method set");
final Method initMethod = (mbd.isNonPublicAccessAllowed() ? Method initMethod = (mbd.isNonPublicAccessAllowed() ?
BeanUtils.findMethod(bean.getClass(), initMethodName) : BeanUtils.findMethod(bean.getClass(), initMethodName) :
ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName)); ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
@ -1798,8 +1796,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return null; return null;
}); });
try { try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
initMethod.invoke(bean), getAccessControlContext()); () -> initMethod.invoke(bean), getAccessControlContext());
} }
catch (PrivilegedActionException pae) { catch (PrivilegedActionException pae) {
InvocationTargetException ex = (InvocationTargetException) pae.getException(); InvocationTargetException ex = (InvocationTargetException) pae.getException();

30
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

@ -236,10 +236,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @throws BeansException if the bean could not be created * @throws BeansException if the bean could not be created
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, protected <T> T doGetBean(
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
final String beanName = transformedBeanName(name); String beanName = transformedBeanName(name);
Object bean; Object bean;
// Eagerly check singleton cache for manually registered singletons. // Eagerly check singleton cache for manually registered singletons.
@ -288,7 +289,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
try { try {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args); checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on. // Guarantee initialization of beans that the current bean depends on.
@ -342,7 +343,10 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
else { else {
String scopeName = mbd.getScope(); String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName); if (!StringUtils.hasLength(scopeName)) {
throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
}
Scope scope = this.scopes.get(scopeName);
if (scope == null) { if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
} }
@ -466,10 +470,12 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
return false; return false;
} }
if (isFactoryBean(beanName, mbd)) { if (isFactoryBean(beanName, mbd)) {
final FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName); FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> return AccessController.doPrivileged(
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) || !fb.isSingleton()), (PrivilegedAction<Boolean>) () ->
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) ||
!fb.isSingleton()),
getAccessControlContext()); getAccessControlContext());
} }
else { else {
@ -1366,7 +1372,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @throws CannotLoadBeanClassException if we failed to load the class * @throws CannotLoadBeanClassException if we failed to load the class
*/ */
@Nullable @Nullable
protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class<?>... typesToMatch) protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch)
throws CannotLoadBeanClassException { throws CannotLoadBeanClassException {
try { try {
@ -1374,8 +1380,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
return mbd.getBeanClass(); return mbd.getBeanClass();
} }
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>) () -> return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>)
doResolveBeanClass(mbd, typesToMatch), getAccessControlContext()); () -> doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
} }
else { else {
return doResolveBeanClass(mbd, typesToMatch); return doResolveBeanClass(mbd, typesToMatch);
@ -1614,7 +1620,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* Get the object for the given bean instance, either the bean * Get the object for the given bean instance, either the bean
* instance itself or its created object in case of a FactoryBean. * instance itself or its created object in case of a FactoryBean.
* @param beanInstance the shared bean instance * @param beanInstance the shared bean instance
* @param name name that may include factory dereference prefix * @param name the name that may include factory dereference prefix
* @param beanName the canonical bean name * @param beanName the canonical bean name
* @param mbd the merged bean definition * @param mbd the merged bean definition
* @return the object to expose for the bean * @return the object to expose for the bean

36
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -284,12 +284,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
* when deciding whether a bean definition should be considered as a * when deciding whether a bean definition should be considered as a
* candidate for autowiring. * candidate for autowiring.
*/ */
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) { public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) {
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null"); Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
if (autowireCandidateResolver instanceof BeanFactoryAware) { if (autowireCandidateResolver instanceof BeanFactoryAware) {
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(DefaultListableBeanFactory.this); ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
return null; return null;
}, getAccessControlContext()); }, getAccessControlContext());
} }
@ -407,8 +407,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
// Check all bean definitions. // Check all bean definitions.
for (String beanName : this.beanDefinitionNames) { for (String beanName : this.beanDefinitionNames) {
// Only consider bean as eligible if the bean name // Only consider bean as eligible if the bean name is not defined as alias for some other bean.
// is not defined as alias for some other bean.
if (!isAlias(beanName)) { if (!isAlias(beanName)) {
try { try {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
@ -505,8 +504,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) public <T> Map<String, T> getBeansOfType(
throws BeansException { @Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
String[] beanNames = getBeanNamesForType(type, includeNonSingletons, allowEagerInit); String[] beanNames = getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
Map<String, T> result = new LinkedHashMap<>(beanNames.length); Map<String, T> result = new LinkedHashMap<>(beanNames.length);
@ -619,12 +618,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
* @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm
* @return whether the bean should be considered as autowire candidate * @return whether the bean should be considered as autowire candidate
*/ */
protected boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver) protected boolean isAutowireCandidate(
String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver)
throws NoSuchBeanDefinitionException { throws NoSuchBeanDefinitionException {
String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName); String bdName = BeanFactoryUtils.transformedBeanName(beanName);
if (containsBeanDefinition(beanDefinitionName)) { if (containsBeanDefinition(bdName)) {
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanDefinitionName), descriptor, resolver); return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(bdName), descriptor, resolver);
} }
else if (containsSingleton(beanName)) { else if (containsSingleton(beanName)) {
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor, resolver); return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor, resolver);
@ -656,8 +656,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd,
DependencyDescriptor descriptor, AutowireCandidateResolver resolver) { DependencyDescriptor descriptor, AutowireCandidateResolver resolver) {
String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName); String bdName = BeanFactoryUtils.transformedBeanName(beanName);
resolveBeanClass(mbd, beanDefinitionName); resolveBeanClass(mbd, bdName);
if (mbd.isFactoryMethodUnique) { if (mbd.isFactoryMethodUnique) {
boolean resolve; boolean resolve;
synchronized (mbd.constructorArgumentLock) { synchronized (mbd.constructorArgumentLock) {
@ -668,7 +668,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
} }
} }
return resolver.isAutowireCandidate( return resolver.isAutowireCandidate(
new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName)), descriptor); new BeanDefinitionHolder(mbd, beanName, getAliases(bdName)), descriptor);
} }
@Override @Override
@ -735,11 +735,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (isFactoryBean(beanName)) { if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) { if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean; FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit; boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) isEagerInit = AccessController.doPrivileged(
((SmartFactoryBean<?>) factory)::isEagerInit, (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext()); getAccessControlContext());
} }
else { else {
@ -761,7 +761,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
for (String beanName : beanNames) { for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName); Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) { if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated(); smartSingleton.afterSingletonsInstantiated();

12
spring-beans/src/main/java/org/springframework/beans/factory/support/FactoryBeanRegistrySupport.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,11 +54,11 @@ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanReg
* or {@code null} if the type cannot be determined yet * or {@code null} if the type cannot be determined yet
*/ */
@Nullable @Nullable
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) { protected Class<?> getTypeForFactoryBean(FactoryBean<?> factoryBean) {
try { try {
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedAction<Class<?>>) return AccessController.doPrivileged(
factoryBean::getObjectType, getAccessControlContext()); (PrivilegedAction<Class<?>>) factoryBean::getObjectType, getAccessControlContext());
} }
else { else {
return factoryBean.getObjectType(); return factoryBean.getObjectType();
@ -153,9 +153,7 @@ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanReg
* @throws BeanCreationException if FactoryBean object creation failed * @throws BeanCreationException if FactoryBean object creation failed
* @see org.springframework.beans.factory.FactoryBean#getObject() * @see org.springframework.beans.factory.FactoryBean#getObject()
*/ */
private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName) private Object doGetObjectFromFactoryBean(FactoryBean<?> factory, String beanName) throws BeanCreationException {
throws BeanCreationException {
Object object; Object object;
try { try {
if (System.getSecurityManager() != null) { if (System.getSecurityManager() != null) {

Loading…
Cancel
Save