Sobes.tech
Junior — Middle

What is the difference between @Controller and @Repository annotations in the Spring framework, and in what situations are they used?

sobes.tech AI

Answer from AI

In Spring, the annotations @Controller and @Repository serve different purposes:

  • @Controller — indicates a class that handles HTTP requests and is responsible for user interaction (often used in MVC for web applications). Such classes return views or data.

  • @Repository — marks a class responsible for database operations (DAO layer). Spring handles such classes in a special way, for example, automatically translating database exceptions into unified Spring exceptions.

Use @Controller for web application controllers, and @Repository for data access components.

What is the difference between @Controller and… - sobes.tech