Skip to content

Commit 5aa1592

Browse files
chenggwangsbrannen
andcommitted
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 <[email protected]> Co-authored-by: Sam Brannen <[email protected]>
1 parent 25b4e29 commit 5aa1592

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
6161

6262
/** Name of the target bean we will create on each invocation. */
6363
@Nullable
64-
private String targetBeanName;
64+
protected String targetBeanName;
6565

6666
/** Class of the target. */
6767
@Nullable

spring-aop/src/main/java/org/springframework/aop/target/AbstractPrototypeBasedTargetSource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
5454
if (!beanFactory.isPrototype(getTargetBeanName())) {
5555
throw new BeanDefinitionStoreException(
5656
"Cannot use prototype-based TargetSource against non-prototype bean with name '" +
57-
getTargetBeanName() + "': instances would not be independent");
57+
this.targetBeanName + "': instances would not be independent");
5858
}
5959
}
6060

@@ -64,7 +64,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
6464
*/
6565
protected Object newPrototypeInstance() throws BeansException {
6666
if (logger.isDebugEnabled()) {
67-
logger.debug("Creating new instance of bean '" + getTargetBeanName() + "'");
67+
logger.debug("Creating new instance of bean '" + this.targetBeanName + "'");
6868
}
6969
return getBeanFactory().getBean(getTargetBeanName());
7070
}
@@ -75,7 +75,7 @@ protected Object newPrototypeInstance() throws BeansException {
7575
*/
7676
protected void destroyPrototypeInstance(Object target) {
7777
if (logger.isDebugEnabled()) {
78-
logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
78+
logger.debug("Destroying instance of bean '" + this.targetBeanName + "'");
7979
}
8080
if (getBeanFactory() instanceof ConfigurableBeanFactory cbf) {
8181
cbf.destroyBean(getTargetBeanName(), target);
@@ -85,7 +85,7 @@ else if (target instanceof DisposableBean disposableBean) {
8585
disposableBean.destroy();
8686
}
8787
catch (Throwable ex) {
88-
logger.warn("Destroy method on bean with name '" + getTargetBeanName() + "' threw an exception", ex);
88+
logger.warn("Destroy method on bean with name '" + this.targetBeanName + "' threw an exception", ex);
8989
}
9090
}
9191
}

spring-aop/src/main/java/org/springframework/aop/target/PrototypeTargetSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void releaseTarget(Object target) {
5454

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

6060
}

spring-aop/src/main/java/org/springframework/aop/target/ThreadLocalTargetSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ThreadLocalTargetSource extends AbstractPrototypeBasedTargetSource
6161
new NamedThreadLocal<>("Thread-local instance of bean") {
6262
@Override
6363
public String toString() {
64-
return super.toString() + " '" + getTargetBeanName() + "'";
64+
return super.toString() + " '" + targetBeanName + "'";
6565
}
6666
};
6767

@@ -86,7 +86,7 @@ public Object getTarget() throws BeansException {
8686
Object target = this.targetInThread.get();
8787
if (target == null) {
8888
if (logger.isDebugEnabled()) {
89-
logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " +
89+
logger.debug("No target for prototype '" + this.targetBeanName + "' bound to thread: " +
9090
"creating one and binding it to thread '" + Thread.currentThread().getName() + "'");
9191
}
9292
// Associate target with ThreadLocal.

0 commit comments

Comments
 (0)