This tutorial explains the advantage of using the spring mvc annotations @Component, @Repository and @Service and how to use them.
Spring Annotations are one of the nice feature to configure the Spring MVC applications. There are wide variety of annotations are define in the Spring Framework. In our earlier tutorials many of the most widely used annotations are explained with more details. Some of them are @Autowired, @Qualifier, @ControlAdvice and @RestController. In this tutorial I would explain the some more spring mvc annotations @Component, @Repository and @Service.
Since Spring Framework 2.5, annotations has been added to Spring Framework as the alternative to the XML configurations. There is more features added in Spring 3.0 like component scanning which automatically scan the packages and load the beans.
- Also Read : Most Popular Spring Tutorials
@Component
This annotation 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 this:
@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 javabeat.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:
<pre>package javabeat.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 } }</pre>
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 javabeat.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 javabeat.net.spring; public interface StudentDAO { public StudentDTO createStudent(); }
StudentDAOImpl.java
package javabeat.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 javabeat.net.spring; public interface StudentManager { public StudentDTO createStudent(); }
StudentManagerImpl.java
package javabeat.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="javabeat.net" /> </beans>
MainApp.java
package javabeat.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("javabeat/net/spring/applicationContext.xml"); StudentManager manager = (StudentManagerImpl)applicationContext.getBean("studentManager"); System.out.println((manager.createStudent())); } }
Summary
I hope this example would have helped you to understand the basic use of @Component, @Service and @Repository spring mvc annotations. These are most widely used for the Spring MVC applications. Also please read our Most Popular Spring Tutorials. If you have any questions, please write it in the comments section.