Skip to content

Make targetBeanName field in AbstractBeanFactoryBasedTargetSource protected to avoid exceptions in logging and toString() #35172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
protected final transient Log logger = LogFactory.getLog(getClass());

/** Name of the target bean we will create on each invocation. */
private @Nullable String targetBeanName;
protected @Nullable String targetBeanName;

/** Class of the target. */
private volatile @Nullable Class<?> targetClass;
Expand Down Expand Up @@ -93,7 +93,7 @@ public String getTargetBeanName() {
Assert.state(this.targetBeanName != null, "Target bean name not set");
return this.targetBeanName;
}

/**
* Specify the target class explicitly, to avoid any kind of access to the
* target bean (for example, to avoid initialization of a FactoryBean instance).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
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");
}
}

Expand All @@ -64,7 +64,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
*/
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());
}
Expand All @@ -75,7 +75,7 @@ protected Object newPrototypeInstance() throws BeansException {
*/
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);
Expand All @@ -85,7 +85,7 @@ else if (target instanceof DisposableBean disposableBean) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void releaseTarget(Object target) {

@Override
public String toString() {
return "PrototypeTargetSource for target bean with name '" + getTargetBeanName() + "'";
return "PrototypeTargetSource for target bean with name '" + this.targetBeanName + "'";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() + " '" + this.targetBeanName + "'";
}
};

Expand All @@ -86,7 +86,7 @@ public Object getTarget() throws BeansException {
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.
Expand Down