All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S6818.html Maven / Gradle / Ivy

The newest version!

Why is this an issue?

@Autowired is an annotation in the Spring Framework for automatic dependency injection. It tells Spring to automatically provide the required dependencies (such as other beans or components) to a class’s fields, methods, or constructors, allowing for easier and more flexible management of dependencies in a Spring application. In other words, it’s a way to wire up and inject dependencies into Spring components automatically, reducing the need for manual configuration and enhancing modularity and maintainability.

In any bean class, only one constructor is permitted to declare @Autowired with the required attribute set to true. This signifies the constructor to be automatically wired when used as a Spring bean. Consequently, when the required attribute remains at its default value (true), only a singular constructor can bear the @Autowired annotation. In cases where multiple constructors have this annotation, they must all specify required=false to be eligible as candidates for auto-wiring.

How to fix it

To maintain code clarity and ensure that the Spring context can create beans correctly, have only one constructor annotated with @Autowired within a Spring component or set required = false.

Code examples

Noncompliant code example

@Component
public class MyComponent {
  private final MyService myService;

  @Autowired
  public MyComponent(MyService myService) {
    this.myService = myService;
    // ...
  }

  @Autowired  // Noncompliant
  public MyComponent(MyService myService, Integer i) {
    this.myService = myService;
    // ...
  }

  @Autowired  // Noncompliant
  public MyComponent(MyService myService, Integer i, String s) {
    this.myService = myService;
    // ...
  }
}

Compliant solution

@Component
public class MyComponent {
  private final MyService myService;

  @Autowired
  public MyComponent(MyService myService) {
    this.myService = myService;
    // ...
  }

  public MyComponent(MyService myService, Integer i) {
    this.myService = myService;
    // ...
  }

  public MyComponent(MyService myService, Integer i, String s) {
    this.myService = myService;
    // ...
  }
}

Noncompliant code example

@Component
public class MyComponent {
  private final MyService myService;

  @Autowired
  public MyComponent(MyService myService) {
    this.myService = myService;
    // ...
  }

  @Autowired  // Noncompliant
  public MyComponent(MyService myService, Integer i) {
    this.myService = myService;
    // ...
  }

  @Autowired  // Noncompliant
  public MyComponent(MyService myService, Integer i, String s) {
    this.myService = myService;
    // ...
  }
}

Compliant solution

@Component
public class MyComponent {
  private final MyService myService;

  @Autowired
  public MyComponent(MyService myService) {
    this.myService = myService;
    // ...
  }

  @Autowired(required=false)  // Compliant
  public MyComponent(MyService myService, Integer i) {
    this.myService = myService;
    // ...
  }

  @Autowired(required=false)  // Compliant
  public MyComponent(MyService myService, Integer i, String s) {
    this.myService = myService;
    // ...
  }
}

Resources

Documentation

Articles & blog posts





© 2015 - 2025 Weber Informatics LLC | Privacy Policy