-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
Conversation
Add a palin method to facilitate the use in situations where the current targetSource information is simply obtained. There is no need to be restricted by the assertions in the original getTargetBeanName method. This is very friendly to the toString method Signed-off-by: chenggwang <[email protected]>
Using getPlainTargetBeanName() instead of getTargetBeanName avoids the influence of assertions on toString Signed-off-by: chenggwang <[email protected]>
Using getPlainTargetBeanName() instead of getTargetBeanName avoids the impact of assertions on the logger Signed-off-by: chenggwang <[email protected]>
Using getPlainTargetBeanName() instead of getTargetBeanName avoids the impact of assertions on the logger Signed-off-by: chenggwang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @chenggwang,
We've decided just to make org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource.targetBeanName
a protected
field instead of introducing a new getPlainTargetBeanName()
method.
Can you please update this PR to do that and then simply reference this.targetBeanName
instead of getPlainTargetBeanName()
?
Thanks
p.s. I've changed the title of this PR to reflect the new focus.
AbstractBeanFactoryBasedTargetSource.targetBeanName
protected to avoid exceptions in logging and toString()
AbstractBeanFactoryBasedTargetSource.targetBeanName
protected to avoid exceptions in logging and toString()
targetBeanName
field in AbstractBeanFactoryBasedTargetSource protected
to avoid exceptions in logging and toString()
Change the modifier of targetBeanName from private to protected to facilitate direct access through this.targetBeanName where no assertion is needed. Signed-off-by: chenggwang <[email protected]>
Since the modifier has been changed to protected, you can directly access targetBeanName using this.targetBeanName Signed-off-by: chenggwang <[email protected]>
Since the modifier has been changed to protected, you can directly access targetBeanName using this.targetBeanName Signed-off-by: chenggwang <[email protected]>
Since the modifier has been changed to protected, you can directly access targetBeanName using this.targetBeanName Signed-off-by: chenggwang <[email protected]>
Hi,@sbrannen //org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource.java
-- private @Nullable String targetBeanName;
++ protected @Nullable String targetBeanName; There are approximately seven places:logging and |
This has been merged into Thanks |
@@ -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 + "'"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Fix #35167
AbstractBeanFactoryBasedTargetSourceCreator
getTargetSource method returns targetSource instance has yet to be completed TargetBeanName Settings, At this point, IDEA or an external tracking tool will call the toString() of targetSource, and toString() will call the getTargetBeanName() method. At this time, the assertion will throw an error.Analysis
The call chain is as follows:
Container by
createBeanFactoryBasedTargetSource
method to obtain a targetSource instance,However, this instance has not completed the configuration Settings, such as setTargetBeanName and setBeanFactory.At this point, when IDEA calls targetSource to print the basic information of the object, it will call getTargetBeanName. At this time, targetBeanName==null. But there is an assertion here:
Assert.state(this.targetBeanName != null, "Target bean name not set")
, An error will be thrown at this pointThe Java® Language Specification:The method toString returns a String representation of the object. Therefore, after an object is instantiated, we should be able to call its toString() smoothly. So, according to the definition of the java specification, when we obtain the current property through the toString() method for display, it should be a simple get method directly obtaining the property, rather than undertaking logical judgment. This violates the single responsibility of toString()
Similarly, we also have multiple loglogger.debug ("No target for prototype '" + getTargetBeanName()).Log recording itself should be a safe and side-effect-free operation. It must not disrupt the normal program flow. If log statements may throw exceptions, they may mask original errors, or even cause cascading failures
The following is the key code segment where the problem occurred:
Solution
So in class AbstractBeanFactoryBasedTargetSourceCreator increase method named getPlainTargetBeanName (),
This simple method is used to provide plain targetBeanName.toString() and both logger calls. When calling methods that require strict non-null judgment, use the original getTargetBeanName()