Sensei Logo Sensei Hub

Recipe - Spring recommendation: @(Rest)Controller, @Service, @Repository should be singletons

Description:
Classes annotated with @(Rest)Controller, @Service, @Repository should have a singleton scope
Level:
warning
Language:
  • java
Tags:
  • web
  • framework specific
  • Spring Boot
  • Spring
  • quality
Documentation

Classes annotated with @Controller, @RestController, @Repository or @Service are singletons by default. Best practices requires them to stay that way.

Before
@Scope("prototype")
@Controller
public class UserController {
}
After
@Controller
public class UserController {
}
Resources
Recipe
id: scw:spring:boot:controller-restcontroller-service-repository-should-be-singletons
version: 10
metadata:
  name: 'Spring recommendation: @(Rest)Controller, @Service, @Repository should be singletons'
  shortDescription: Classes annotated with @(Rest)Controller, @Service, @Repository should have a singleton scope
  level: warning
  language: java
  enabled: true
  descriptionFile: descriptions/Spring_recommendation____Rest_Controller___Service___Repository_should_be_singletons.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
    without:
      parameters:
      - value:
          value:
            stringified: singleton
availableFixes:
- name: Remove the annotation (defaults to singleton scope)
  actions:
  - remove:
      target: self