When we define the annotations @Component, @Service, @Controller, @Repository with the classes, Spring automatically scan and identifies those classes and register the Bean definition with the ApplicationContext.
Difference between @Component, @Repository, @Controller & @Service annotations in Spring:
The main difference between @Component, @Repository, @Controller & @Service annotations is that these are used for different classification.
In our application we can have different layers like presentation, service, business, data access etc. To annotate a class for auto-detection by Spring, we should use the respective annotations as below.
@Component - generic and can be used across the application. It is a basic auto component scan annotation, it indicates annotated class is a auto scan component.
@Service - annotate classes at service layer level ( Indicates a Service component in the business layer).
@Controller - annotated class indicates that it is a controller components, and mainly used at presentation layer.
@Repository - a data access object (Indicates DAO component in the persistence layer which will act as database repository).
We can use @Component for any auto component scan, but for better readability and better standards we should use respective annotations. @Repository, @Controller & @Service annotations serve as specializations of @Component annotation.
Example of @Service and @Repository:
DAO layer
Define the DAO class as below:
EmployeeDAO.java
package com.example.employee.dao;
@Repository
public class EmployeeDAO
{
@Override
public String toString() {
return "Hello , We are in EmployeeDAO";
}
}
Service layer
Define Service class as below:
EmployeeService.java
package com.example.employee.services;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.employee.dao.EmployeeDAO;
@Service
public class EmployeeService
{
@Autowired
EmployeeDAO employeeDAO;
@Override
public String toString() {
return "EmployeeService [employeeDAO=" + employeeDAO + "]";
}
}
Now define the below code in app-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.example"/>
</beans>
Now define the below class to run the code:
App.java
package com.example.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.employee.services.EmployeeService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"app-context.xml"});
EmployeeService emp = (EmployeeService)context.getBean("employeeService");
System.out.println(emp);
}
}
Output
EmployeeService [employeeDAO=Hello , This is EmployeeDAO]
Hope this will help you :)
1 Comment(s)