Spring Annotations
The annotations a Spring developer reaches for every day.
Stereotypes & configuration
@Component
generic Spring-managed bean@Service
business-logic bean (semantic)@Repository
data access; translates persistence exceptions@Configuration
class that declares @Bean methods@Bean
method whose return value is a bean@ConfigurationProperties("app")bind typed config
Boot & context
@SpringBootApplication
config + auto-config + scan@ComponentScan
where to look for beans@Value("${app.name}")inject a single property@Profile("prod")bean active only in a profile@ConditionalOnMissingBean
back off if user defined one
Web (Spring MVC)
@RestController
@Controller + @ResponseBody@RequestMapping("/api")base path for a controller@GetMapping @PostMapping @PutMapping @DeleteMapping
@PathVariable @RequestParam @RequestBody
bind request data@Valid
trigger Bean Validation on the body@RestControllerAdvice
global exception handling
Data & transactions
@Entity @Id @GeneratedValue
JPA mapping@ManyToOne @OneToMany @ManyToMany
relationships@Query("select b from Book b …")custom JPQL/SQL@Transactional
wrap a method in a DB transaction@Transactional(readOnly = true)
read optimization
Security & testing
@EnableWebSecurity
turn on the security config@PreAuthorize("hasRole('ADMIN')")method-level rule@SpringBootTest
full-context integration test@WebMvcTest @DataJpaTest
focused slice tests@MockitoBean
replace a bean with a mock (Boot 3.4+)