Make targetBeanName field in AbstractBeanFactoryBasedTargetSource protected

Prior to this commit, subclasses of AbstractBeanFactoryBasedTargetSource
referenced the targetBeanName via getTargetBeanName() which throws an
IllegalStateException if the targetBeanName has not yet been set.

This commit changes the visibility of the targetBeanName field from
private to protected in order to facilitate direct access through
this.targetBeanName where no assertion is needed.

By doing so, we avoid exceptions in logging and toString()
implementations in subclasses.

Closes gh-35172

Signed-off-by: chenggwang <90715678+chenggwang@users.noreply.github.com>
Co-authored-by: Sam Brannen <104798+sbrannen@users.noreply.github.com>
This commit is contained in:
chenggwang
2025-07-11 15:51:10 +02:00
committed by Sam Brannen
co-authored by Sam Brannen
parent 25b4e29f5e
commit 5aa15923cf
4 changed files with 8 additions and 8 deletions
@@ -61,7 +61,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
/** Name of the target bean we will create on each invocation. */
@Nullable
private String targetBeanName;
protected String targetBeanName;
/** Class of the target. */
@Nullable
@@ -54,7 +54,7 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
if (!beanFactory.isPrototype(getTargetBeanName())) {
throw new BeanDefinitionStoreException(
"Cannot use prototype-based TargetSource against non-prototype bean with name '" +
getTargetBeanName() + "': instances would not be independent");
this.targetBeanName + "': instances would not be independent");
}
}
@@ -64,7 +64,7 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
*/
protected Object newPrototypeInstance() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Creating new instance of bean '" + getTargetBeanName() + "'");
logger.debug("Creating new instance of bean '" + this.targetBeanName + "'");
}
return getBeanFactory().getBean(getTargetBeanName());
}
@@ -75,7 +75,7 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
*/
protected void destroyPrototypeInstance(Object target) {
if (logger.isDebugEnabled()) {
logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
logger.debug("Destroying instance of bean '" + this.targetBeanName + "'");
}
if (getBeanFactory() instanceof ConfigurableBeanFactory cbf) {
cbf.destroyBean(getTargetBeanName(), target);
@@ -85,7 +85,7 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
disposableBean.destroy();
}
catch (Throwable ex) {
logger.warn("Destroy method on bean with name '" + getTargetBeanName() + "' threw an exception", ex);
logger.warn("Destroy method on bean with name '" + this.targetBeanName + "' threw an exception", ex);
}
}
}
@@ -54,7 +54,7 @@ public class PrototypeTargetSource extends AbstractPrototypeBasedTargetSource {
@Override
public String toString() {
return "PrototypeTargetSource for target bean with name '" + getTargetBeanName() + "'";
return "PrototypeTargetSource for target bean with name '" + this.targetBeanName + "'";
}
}
@@ -61,7 +61,7 @@ public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
new NamedThreadLocal<>("Thread-local instance of bean") {
@Override
public String toString() {
return super.toString() + " '" + getTargetBeanName() + "'";
return super.toString() + " '" + targetBeanName + "'";
}
};
@@ -86,7 +86,7 @@ public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
Object target = this.targetInThread.get();
if (target == null) {
if (logger.isDebugEnabled()) {
logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " +
logger.debug("No target for prototype '" + this.targetBeanName + "' bound to thread: " +
"creating one and binding it to thread '" + Thread.currentThread().getName() + "'");
}
// Associate target with ThreadLocal.