build failed Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES how to fix spot bug reported issue

huangapple go评论109阅读模式
英文:

build failed Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES how to fix spot bug reported issue

问题

我遇到了Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES 错误,并且在 setter 方法中构建失败。这是由 spotbugs 报告的。如何修复这个问题?请帮忙,因为找不到解决方案。
以下是类 MySuperServiceConfig 的代码。

@Component
@ConfigurationProperties("mysuperservice")
@PropertySource("classpath:data.properties")
public class MySuperServiceConfig {
  private String username;
  private String password;
  private List<String> schemadata;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public List<String> getSchemadata() {
    return schemadata;
  }

  public void setSchemadata(List<String> schemadata) {
    this.schemadata = schemadata;
  }
}

以下是日志内容。

[INFO] --- spotbugs-maven-plugin:3.1.12.2:check (default) @ aif-handler ---
[INFO] BugInstance size is 3
[INFO] Error size is 0
[INFO] Total bugs: 3
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setPassword(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setSchemadata(List) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setUsername(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
英文:

I am facing Error: java: USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES and build gets failed for setter method. it is reported by spotbugs. How to fix this please help as not getting solution.
Below is the class MySuperServiceConfig.

@Component
@ConfigurationProperties(&quot;mysuperservice&quot;)
@PropertySource(&quot;classpath:data.properties&quot;)
public class MySuperServiceConfig {
  private String username;
  private String password;
  private List&lt;String&gt; schemadata;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public List&lt;String&gt; getSchemadata() {
    return schemadata;
  }

  public void setSchemadata(List&lt;String&gt; schemadata) {
    this.schemadata = schemadata;
  }
}

Below is the log

[INFO] --- spotbugs-maven-plugin:3.1.12.2:check (default) @ aif-handler ---
[INFO] BugInstance size is 3
[INFO] Error size is 0
[INFO] Total bugs: 3
[ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setPassword(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES [ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setSchemadata(List) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES [ERROR] Method com.test.service.fileparser.MySuperServiceConfig.setUsername(String) of Singleton class writes to a field in an unsynchronized manner [com.amex.scs.aif.service.fileparser.MySuperServiceConfig] At MySuperServiceConfig.java:
USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES

答案1

得分: 1

The spotbug documentation has some information on this error

http://fb-contrib.sourceforge.net/bugdescriptions.html

USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES

This method writes to a field of this class. Since this class is seen
as a Singleton this can produce race conditions, or cause non-visible
changes to other threads, because the field isn't accessed
synchronously.

由于 Configuration Property 类被标记为 Component 注解,Spotbug 将知道这是一个 Singleton Bean。Configuration bean 没有自动装配任何字段 - username / password / schemaData,但它有 getter 和 setter 方法。因此,代码中可能多次更改这些 bean 的实例属性或依赖关系,这可能导致竞争条件。在这种情况下,解决方法应该是移除这个 Component 注解,因为这是一个 Configuration Property 映射类。

为了进行自动装配,您可以:

  1. 在 SpringBootApplication 类或另一个 Configuration 类上使用 @EnableConfigurationProperties(MySuperServiceConfig.class)
  2. 将其标记为 @Configuration 类
英文:

The spotbug documentation has some information on this error

http://fb-contrib.sourceforge.net/bugdescriptions.html

> USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
>
> This method writes to a field of this class. Since this class is seen
> as a Singleton this can produce race conditions, or cause non-visible
> changes to other threads, because the field isn't accessed
> synchronously.

As the Configuration Property class is marked with Component annotation, Spotbug would know that this is a Singleton Bean. The Configuration bean is not autowiring any of the fields - username / password / schemaData, but it has a getter and setter methods. So, it is appearing like these instance properties of the bean or dependancies could be changed multiple times by the code. In such a scenario, there could be race conditions as the methods are not synchronized.
Hence, the fix should be to remove this Component annotation as this is a Configuration Property mapper class.

To have this autowired, you can -

  1. use @EnableConfigurationProperties(MySuperServiceConfig.class) on SpringBootApplication class or another Configuration class
  2. Mark this as a @Configuration class

huangapple
  • 本文由 发表于 2020年7月26日 03:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定