Spring Annotations

Ebru
Koçfinans Tech
Published in
2 min readMar 29, 2022

Hello everyone, I would like to focus on what is the difference between a @controller, @component, @repository, @service, and these kinds of annotations in the Spring.

@Component

It signifies the fact that something is a being that needs to be managed by Spring. It means “Spring, you are in control of this bean.”

@component is the most generic abstraction. When we talk about Web applications, we would talk about multiple layers. We can use @component on any of the components in any of these layers. So it’s very generic.

@Component
public class ContactResource {
...
}

@Controller

It is typically used in the web layer @controller signifies something which is using the MVC pattern, model view controller part.

@Controller
public class CompanyController {
...
}

@Repository

We would use @repository typically on things like a DAO (data access object ). So these are the things that are related to getting the data from our database.

Therefore when we tag any component with @repository, Spring would automatically add the exception Translation for JDBC exceptions.
Whenever a JDBC exception happens, then it needs to be translated into the specific spring exception.
Thus if we put @repository on top of any component, then that happens automatically.

@Repository
public class CompanyDAOImpl implements CompanyDAO {
...
}

@Service

It is typically used in the business layer. In our business logic, the most important function that we would want to provide is transaction management, For all our facade objects which are present in the business layer, we would use @service.

@Service
public class CompanyServiceImpl implements CompanyService {
...
}
Web Applications Multiple Layers

--

--