0

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("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;
  }
}

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:[line 29] 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:[line 37] 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:[line 21] USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES
Kim
  • 305
  • 1
  • 3
  • 14
  • I think you should not use Component annotation on this class. Can you try removing it and check ? – SKumar Jul 25 '20 at 21:02
  • Yes it resolved the build failed issue but after removing Component Annotation it is not available for Autowiring and getting runtime error error Field myAppProperties in com.test.AppController required a bean of type 'com.test.service.fileparser.MySuperServiceConfig' that could not be found. – Kim Jul 26 '20 at 11:58
  • @user11244881 Thank you it resolved the issue and after using the annotation Configuration it is available for Autowiring too. – Kim Jul 26 '20 at 12:45
  • cool, I will add an answer why I believe Component annotation was the issue – SKumar Jul 26 '20 at 12:53

1 Answers1

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.

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
SKumar
  • 1,940
  • 1
  • 7
  • 12