mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.72k stars 12.82k forks source link

Optimize `SystemMetaObject.forObject` Usage by Reusing `DefaultReflectorFactory` #3231

Closed qingbozhang closed 1 month ago

qingbozhang commented 1 month ago

Hi, While using SystemMetaObject.forObject inside the Interceptor, I discovered that each call creates a new DefaultReflectorFactory instance, which prevents the reuse of the reflector cache.

To address this, I defined a default DEFAULT_REFLECTOR_FACTORY to facilitate cache reuse. Additionally, I added support for custom parameters, allowing users to pass the DefaultReflectorFactory from Configuration to maximize cache efficiency.

here is the detail from JFR WechatIMG131

harawata commented 1 month ago

Hello @qingbozhang ,

I've seen some interceptor implementations using MetaObject as a reflection utility, but you should not use MetaObject in your code. It's an internal utility and is not designed/optimized for a generic use.

qingbozhang commented 1 month ago

At the very least, the comments should clearly state that it is an internal tool and is not recommended for external use.

It is advised not to rely on conventions to reach a consensus, but to enforce its package visibility. This is a poor design.

As far as I know, many people are not aware of this, and I have found many tutorials that use it, even answers from chatbots like ChatGPT also suggest it.

harawata commented 1 month ago

Well, you should contact the authors of those tutorials. The interceptors that I saw used the MetaObject for overwriting field value. It may not be obvious to the readers of those tutorials, but it can be done by basic reflection and I'm not interested in adding obvious comments to the classes in this repo only because someone wrote a bad tutorial.

qingbozhang commented 1 month ago

Thank you for your personal subjective advice. This is a project that does not embody the spirit of open source.

harawata commented 1 month ago

Just to be clear, I am sincerely asking you to contact the authors of those tutorials to improve the contents. That is one way to contribute to the open source project.