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

Spring "@Controller" classes should not use "@Scope"

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this "@Scope" annotation.
    • Highlighting:
      Hide

      @Scope(...)

      Show
      @Scope(...)
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources

      Description

      Spring @Controller}}s, {{@Service}}s, and {{@Repository}}s have {{singleton scope by default, meaning only one instance of the class is ever instantiated in the application. Defining any other scope for one of these class types will result in needless churn as new instances are created and destroyed. In a busy web application, this could cause a significant amount of needless additional load on the server.

      This rule raises an issue when the @Scope annotation is applied to a @Controller, @Service, or @Repository with any value but "singleton". @Scope("singleton") is redundant, but ignored.

      Noncompliant Code Example

      @Scope("prototype")  // Noncompliant
      @Controller
      public class HelloWorld { 
      

      Compliant Solution

      @Controller
      public class HelloWorld { 
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: