Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5673

Spring components should use specialized annotations

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Use @<SpecializedAnnotation> instead of @Component
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Semantic Analysis
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      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

      @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 {
         // ...
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                tibor.blenessy Tibor Blenessy
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: