
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
77 lines
1.6 KiB
Plaintext
77 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
||
|
||
The Spring Framework provides several specializations of the generic ``++@Component++`` stereotype annotation which better express the programmer’s intent. Using them should be preferred.
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
[source,java]
|
||
----
|
||
@Component // Noncompliant; class name suggests it's a @Service
|
||
public class CustomerServiceImpl {
|
||
// ...
|
||
}
|
||
|
||
@Component // Noncompliant; class name suggests it's a @Repository
|
||
public class ProductRepository {
|
||
// ...
|
||
}
|
||
|
||
@Component // Noncompliant; class name suggests it's a @Controller or @RestController
|
||
public class FooBarRestController {
|
||
// ...
|
||
}
|
||
----
|
||
|
||
|
||
=== Compliant solution
|
||
|
||
[source,java]
|
||
----
|
||
@Service // Compliant
|
||
public class CustomerServiceImpl {
|
||
// ...
|
||
}
|
||
|
||
@Repository // Compliant
|
||
public class ProductRepository {
|
||
// ...
|
||
}
|
||
|
||
@RestController // Compliant
|
||
public class FooBarRestController {
|
||
// ...
|
||
}
|
||
|
||
@Component // Compliant
|
||
public class SomeOtherComponent {
|
||
// ...
|
||
}
|
||
----
|
||
|
||
|
||
== Resources
|
||
|
||
* https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-stereotype-annotations[Spring documentation - @Component and Further Stereotype Annotations]
|
||
|
||
|
||
ifdef::env-github,rspecator-view[]
|
||
|
||
'''
|
||
== Implementation Specification
|
||
(visible only on this page)
|
||
|
||
=== Message
|
||
|
||
Use @<SpecializedAnnotation> instead of @Component
|
||
|
||
|
||
'''
|
||
== Comments And Links
|
||
(visible only on this page)
|
||
|
||
=== on 11 Dec 2019, 16:42:36 Tibor Blenessy wrote:
|
||
Based on community suggestion \https://community.sonarsource.com/t/use-specializations-of-component-for-specific-use-cases/12379
|
||
|
||
endif::env-github,rspecator-view[]
|