Why is this an issue?

The Spring Framework provides several specializations of the generic @Component stereotype annotation: @Service, @Repository, @Controller, and @RestController. Using the appropriate specialization instead of the generic @Component has concrete benefits:

This rule raises an issue when a class is annotated with @Component and its name ends with a suffix that suggests a more specific stereotype: Service, ServiceImpl, Repository, Controller, or RestController.

Noncompliant code example

@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

@Service // Compliant
public class CustomerServiceImpl {
  // ...
}

@Repository // Compliant
public class ProductRepository {
    // ...
}

@RestController // Compliant
public class FooBarRestController {
    // ...
}

@Component // Compliant
public class SomeOtherComponent {
   // ...
}

Exceptions

This rule does not raise an issue when the class name does not end with one of the recognized suffixes (Service, ServiceImpl, Repository, Controller, RestController). For example, a class named EventProcessor annotated with @Component does not trigger this rule, even if it could arguably be a @Service.

The rule does not suggest @Controller or @RestController unless the class contains at least one method annotated with a request mapping annotation (@RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping). Classes named "Controller" that do not handle HTTP requests are not flagged.

The rule does not suggest @Controller or @RestController for classes that belong to non-web Spring infrastructure:

The rule does not raise an issue when the class already carries a specialized stereotype annotation (@Controller, @RestController, @Service, @Repository) alongside @Component.

If your class intentionally uses @Component despite its name (for example, a multi-role bean or a class whose name coincidentally contains a suffix), mark the issue as "Won’t Fix". A genuine false positive would be a case where the rule suggests a stereotype that does not match the class’s actual role.

Resources

Documentation