Mastering Spring Annotations: From @Controller to @CacheEvict

This article provides a comprehensive guide to essential Spring annotations—including @Controller, @RestController, @Service, @Autowired, @RequestMapping, @ModelAttribute, @Cacheable, @CacheEvict, @Resource, @PostConstruct, @PreDestroy, @Repository, @Component, @Scope, @SessionAttributes, @Required and @Qualifier—explaining their purposes, usage patterns, and practical examples with accompanying diagrams.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Mastering Spring Annotations: From @Controller to @CacheEvict

Spring offers a rich set of annotations that simplify configuration and reduce boilerplate code. Below is a concise reference for the most commonly used annotations, each illustrated with a diagram.

@Controller

Marks a class as a Spring MVC controller that handles HTTP requests.

@RestController

Introduced after Spring 4, it combines @Controller and @ResponseBody, so methods return JSON by default without needing @ResponseBody.

@Service

Designates a business‑logic component, allowing the class to be auto‑detected and injected into Spring configuration.

@Autowired

Injects a bean by type (or by name if required=false). It can be placed on fields or methods.

@RequestMapping

Provides URL mapping at class level (relative to the web root) and method level (relative to the class mapping).

@RequestParam

Maps request parameters to method arguments.

@ModelAttribute

Can be placed on a method or a method parameter. When on a method, its return value is added to the model; when on a parameter, the request data is bound to the object and added to the model automatically.

@Cacheable

Enables caching for a method or class. Example: @Cacheable(value="UserCache") caches the method result using the method arguments as the key.

@CacheEvict

Marks a method that clears entries from the specified cache, e.g., @CacheEvict(value="UserCache").

@Resource

Similar to @Autowired but defaults to injection by name; can also specify name or type attributes.

@PostConstruct

Executes the annotated method once after the bean is instantiated and dependencies are injected, typically for initialization tasks.

@PreDestroy

Runs the annotated method once before the bean is removed from the container, useful for cleanup.

@Repository

Marks a data‑access component (DAO) for exception translation.

@Component

General‑purpose stereotype for any Spring-managed component.

@Scope

Defines bean scope: singleton, prototype, request, session, or global session.

@SessionAttributes

Stores selected model attributes in the HTTP session to survive across multiple requests.

@Required

Applied to bean property setters; the property must be populated in XML configuration, otherwise a BeanInitializationException is thrown.

@Qualifier

Used together with @Autowired to disambiguate which bean should be injected when multiple candidates of the same type exist.

Original source: https://m.toutiaocdn.com/i6693736960273416712
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaspringannotationsdependency-injectionSpring MVC
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.