Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ public String getTargetBeanName() {
return this.targetBeanName;
}

/**
* Return the name of the target bean in the factory.
* If the configuration has not been completed yet, null can be returned
* If it's just for obtaining simple basic information, using it is very friendly.
* For example, toString().However, if strict non-null judgment is required,
* please use {@link #getTargetBeanName}
*/
public String getPlainTargetBeanName() {
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");
getPlainTargetBeanName() + "': 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 '" + getPlainTargetBeanName() + "'");
}
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 '" + getPlainTargetBeanName() + "'");
}
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 '" + getPlainTargetBeanName() + "' 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 '" + getPlainTargetBeanName() + "'";
}

}
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() + " '" + getPlainTargetBeanName() + "'";
}
};

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 '" + getPlainTargetBeanName() + "' bound to thread: " +
"creating one and binding it to thread '" + Thread.currentThread().getName() + "'");
}
// Associate target with ThreadLocal.
Expand Down