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 + "'";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that this does not compile with this..

In the future, please run a full local build before pushing changes to a PR.

For this particular case, I fixed the issue while merging this PR onto 6.2.x, so this has been taken care of.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I thought can be completed with simple text editor, I know the inner class general use ThreadLocalTargetSource.this.targetBeanName , The grammar plugin reminds me that perhaps what I want to use is: this. I thought this was a new grammar,I followed the advice of the plugin!!! This is a stupid mistake😭. In the future,i will run a full local build! Sorry to have caused you trouble! @sbrannen

}
};

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