In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to solve the common method call static attribute Sonar problem, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Problem: Sonar thinks there is a problem when a static property is called by an ordinary method
This is what we often use to implement the Bean factory of Spring, write a utility class, and open a static method: get the entity class.
Public class BeanHelper implements BeanFactoryAware {private static BeanFactory factory; / * get entity class * * @ param id * @ return * / public static T getBean (String id) {return (T) factory.getBean (id) } / * set bean factory * * @ param beanFactory * / @ Override public void setBeanFactory (BeanFactory beanFactory) {BeanHelper.factory = beanFactory;}}
Sonar scan will report:
Instance methods should not write to "static" fields Correctly updating a static field from a non-static method is tricky to get right and could easily lead to bugs if there are multiple class instances and/or multiple threads in play. Ideally, static fields are only updated from synchronized static methods. This rule raises an issue each time a static field is updated from a non-static method.
Translation:
Updating static fields correctly from non-static methods is tricky, and if you have multiple class instances and / or threads running, you can easily cause bug. Ideally, static fields are updated only from synchronous static methods. This rule raises a problem each time a static field is updated from a non-static method.
Solution: wrap a static method public class BeanHelper implements BeanFactoryAware {private static BeanFactory factory; / * to get the entity class * * @ param id * @ return * / public static T getBean (String id) {return (T) factory.getBean (id);} private static void setFactory (BeanFactory factory) {BeanHelper.factory = factory } / * set bean factory * * @ param beanFactory * / @ Override public void setBeanFactory (BeanFactory beanFactory) {setFactory (beanFactory);}}
Wrapping a static setFactory method, Sonar scanning will not cause a problem. Because it is a static method, it will be initialized first. Further, in the setFactory method, you can determine whether BeanHelper.factory is null, and if it is not null, it will not be assigned, to ensure that it will not be overwritten by multiple instances or multi-threads, so it will not be written out in detail here.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.