137 lines
3.6 KiB
Plaintext
137 lines
3.6 KiB
Plaintext
== 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
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@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
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@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
|
|
|
|
[source,java,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
@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
|
|
|
|
[source,java,diff-id=2,diff-type=compliant]
|
|
----
|
|
@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
|
|
|
|
* https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/autowired.html[Spring Framework - Using @Autowired]
|
|
|
|
=== Articles & blog posts
|
|
|
|
* https://www.baeldung.com/spring-autowire[Baeldung - Guide to Spring @Autowired]
|