Spring MVC Annotations : @Component, @Repository, @Service
@Component
@Component tells the spring container to pick the bean while component-scanning is running. It is more generalized form of the annotations and can be used for any type of beans to inform the component-scanning mechanism. The declaration would look like
In reality, @Component annotation is very rarely used since other specialized annotations @Service, @Repository and @Controller can be used.
@Repository
@Repository is specialized implementation of @Component for indicating the Data Access Objects (DAO). Advantage of using the @Repository annotation, importing the DAOs into the DI container and also this annotation makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
@Service
This another specialized version of @Component to inform the spring component-scanning mechanism to load the service classes. These are introduced to in the spring framework to add any specific features to the service classes in the future. The declaration of @Service annotation would be:
Spring MVC Annotations Example
Here is the complete working example using the annotations explained in the above sections. This example uses the spring mvc annotations @Component, @Repository and @Service.
@Component
@Component tells the spring container to pick the bean while component-scanning is running. It is more generalized form of the annotations and can be used for any type of beans to inform the component-scanning mechanism. The declaration would look like
@Component("studentDAO") public class StudentDAOImpl implements StudentDAO{ public StudentDTO createStudent(){ //code here } }
In reality, @Component annotation is very rarely used since other specialized annotations @Service, @Repository and @Controller can be used.
@Repository
@Repository is specialized implementation of @Component for indicating the Data Access Objects (DAO). Advantage of using the @Repository annotation, importing the DAOs into the DI container and also this annotation makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
package aid.net.spring; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; //@Component("studentDAO") - This also correct @Repository("studentDAO") public class StudentDAOImpl implements StudentDAO{ //code here } }
@Service
This another specialized version of @Component to inform the spring component-scanning mechanism to load the service classes. These are introduced to in the spring framework to add any specific features to the service classes in the future. The declaration of @Service annotation would be:
package aid.net.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; //@Component("studentManager") - This also correct @Service("studentManager") public class StudentManagerImpl implements StudentManager{ //code here } }
Spring MVC Annotations Example
Here is the complete working example using the annotations explained in the above sections. This example uses the spring mvc annotations @Component, @Repository and @Service.
StudentDTO.java package aid.net.spring; public class StudentDTO { private String id; private String name; private String grade; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String toString(){ StringBuffer buffer = new StringBuffer(); buffer.append("\nStudent Id : " + this.id); buffer.append("\nStudent Name : " + this.name); buffer.append("\nStudent Grade : " + this.grade); return buffer.toString(); } } StudentDAO.java package aid.net.spring; public interface StudentDAO { public StudentDTO createStudent(); } StudentDAOImpl.java package aid.net.spring; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; //@Component("studentDAO") - This also correct @Repository("studentDAO") public class StudentDAOImpl implements StudentDAO{ public StudentDTO createStudent(){ StudentDTO dto = new StudentDTO(); dto.setId("001"); dto.setName("Che"); dto.setGrade("First"); return dto; } } StudentManager.java package aid.net.spring; public interface StudentManager { public StudentDTO createStudent(); } StudentManagerImpl.java ackage aid.net.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; //@Component("studentManager") - This also correct @Service("studentManager") public class StudentManagerImpl implements StudentManager{ @Autowired StudentDAO studentDAO; public StudentDTO createStudent(){ return studentDAO.createStudent(); } }
applicationContext.xml
xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="aid.net" />
</beans>
MainApp.java package aid.net.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aid/net/spring/applicationContext.xml"); StudentManager manager = (StudentManagerImpl)applicationContext.getBean("studentManager"); System.out.println((manager.createStudent())); } }