Sensei Logo Sensei Hub

Recipe - Code quality: Do not add @Scope("singleton") on a Spring (rest)controller, service or repository

Description:
Adding @Scope("singleton") is redundant
Level:
info
Language:
  • java
Tags:
  • web
  • framework specific
  • Spring Boot
  • Spring
  • quality
Documentation

Applying the @Scope("singleton") to a class annotated with @Controller, @RestController, @Repository or @Service is redundant as these classes are singletons by default.

Before
@Scope("singleton")
@Controller
public class UserController {
}
After
@Controller
public class UserController {
}
Resources
Recipe
id: scw:spring:boot:default-singleton-scope-singleton-annotation-redundant
version: 10
metadata:
  name: 'Code quality: Do not add @Scope("singleton") on a Spring (rest)controller, service or repository'
  shortDescription: Adding @Scope("singleton") is redundant
  level: info
  language: java
  enabled: true
  descriptionFile: descriptions/Code_quality__Do_not_add__Scope__singleton___on_a_Spring__rest_controller__service_or_repository.html
  tags: web;framework specific;Spring Boot;Spring;quality
search:
  annotation:
    in:
      typeDeclaration:
        annotation:
          type:
            reference:
              matches: Controller|RestController|Repository|Service
            checkInheritance: true
    type: Scope
    parameters:
    - value:
        value:
          stringified: singleton
availableFixes:
- name: Remove redundant annotation
  actions:
  - remove:
      target: self