Spring @Component, @Service, @Repository, @Controller Difference

Spring @Component, @Service, @Repository and @Controller annotations are used for automatic bean detection using classpath scan in Spring framework. @Component is a generic annotation. Difference of @Service, @Repository, @Controller with @Component is they are special cases of @Component and used for particular purposes. The difference is just classification only.

For all these annotations (stereotypes), technically the core purpose is same. Spring automatically scans and identifies all these classes that are annotated with “ @Component, @Service, @Repository, @Controller”  and registers BeanDefinition with ApplicationContext. We read about @Controller in a previous Spring tutorial on annotation based controllers.

Difference Between @Component, @Service, @Repository and @Controller

Spring Configuration for Component-scan

For these beans to be instantiated by Spring, we need to have the following configuration in the spring configuration XML. Assuming com.javapapers.spring is a base package containing these classes. Needless to say, these Java classes should be part of the application classpath.

<?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.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               
     <context:component-scan base-package="com.javapapers.spring"/>
     
</beans>

Disable automatic scan of @Component, @Repository, @Service, @Controller:

This automatic scan behavior can be disabled for the default stereotypes by setting the use-default-filters property to false,

<beans>

<context:component-scan use-default-filters = "false" base-package="com.javapapers.spring" />

</beans>

Customize Default Spring Scan Behavior

When component-scan is enable, by default spring scans for @Component, @Service, @Repository and @Controller stereotypes only. All the classes present in the classpath coming under the base-package annotated with these stereotypes will be auto-detected. We can modify this default Spring behavior using include-filter or exclude-filter attribute of the component-scan attribute. There are five filter types available ‘annotation, assignable, aspectj, regex, custom’ and they can be used as below,

<beans>

   <context:component-scan base-package="com.javapapers.spring">
      <context:include-filter type="regex" expression=".*Stub.*Repository"/>
      <context:exclude-filter type="annotation"
                              expression="org.springframework.stereotype.Repository"/>
      <context:include-filter type="assignable" expression="com.javapapers.spring.AnimalService"/>
   </context:component-scan>

</beans>

Bean Naming

For all @Component, @Service, @Repository and @Controller stereotyped components the bean name is assigned based on BeanNameGenerator strategy. We can also supply our name choice during annotation and that will take high precedence.

@Service("lionKing")
public class LionService {
  // ...
}

@Repository
public class AnimalJpa {
  // ...
}

For LionService the instantiated bean name will be lionKing which we have supplied, but in the case of AnimalJpa the bean name will be animalJpa. This behavior is same for all sterotypes @Component, @Service, @Repository and @Controller.

This Spring tutorial was added on 30/10/2013.

Comments on "Spring @Component, @Service, @Repository, @Controller Difference" Tutorial:

  1. Siva says:

    Good One!

  2. vishal says:

    Excellent way of explaination.. Too good

  3. Maryam says:

    Thanks Joe,you explain concepts very well.I enjoy read those and also I am learning many things.

  4. Mohammed Chand says:

    Good one.

  5. Anonymous says:

    but when are these beans instantiated

  6. irfan says:

    Nice article ,but 1 query,when does spring container instantiate the bean?

  7. Pratik says:

    Nicely Explained

  8. Anonymous says:

    Please write some Spring and Hibernate articles for beginners. I am a beginner and I am having a hard time understanding the spring and hibernate concepts.

  9. sridhar reddy says:

    its very clear and nicely explined.
    Thanks a lot.

  10. ravi says:

    good…

  11. Anonymous says:

    good guid

  12. Madan mohan Behera says:

    Very good article .

  13. Prakash says:

    Very good one.

  14. Pankaj Lilhare says:

    Excellent Joe!

  15. Eairrick says:

    Excellent succinct overview…Thanks

  16. Anonymous says:

    not clear as it does not give more detils

  17. Lalatendu Guru says:

    Good job!

  18. Satya says:

    Hi Joe

    Good site.

    But just wanted to bring to your attention regarding the code formatter plugin that you are susing on your site.

    It is not displaying the code properly

  19. vageesh bhat says:

    superb explaination boss… thank u

  20. Anonymous says:

    very informative

  21. Anonymous says:

    New to SPRING…This is great..GOOD expalnation…KEEP IT UP (y)

  22. Anonymous says:

    it depends on usage of container.we have two types of ioc containers in spring BeanFactory,ApplicationContext.when a request comes bean factory creates the instance which we declare a bean in xml.but application context creates all bean instances at the time of loading..

  23. Anonymous says:

    Nice way to explain.. it fills the knowledge gap,,,

  24. Dung Tran says:

    Hi Joe,
    I really like it, It’s very helpful for me. Thank a lot.
    I try to find the topic about Spring transaction in your website. But I can not, If you have time pls make a topic about it.

    Best thank,

  25. Anonymous says:

    Couldnt understand images

  26. Anonymous says:

    how can I config a bean with more than one name using annotation like xml.
    for xml example:

  27. Md Javed says:

    Hi,

    I am not clear with your explanation on @Repository and @Service annotation can you please elaborate on that or post some small snippet of code for better understanding .

  28. Girish says:

    Its really help me to get clear idea about core annotations

    Thanks a lot

  29. Renuka says:

    Good One!

  30. mani says:

    Thank you!!

  31. Anonymous says:

    Good explanation.

    But, if you explain differences in above annotations with example then it is much clear for beginners. I’ll wait for update on this. I would love to read it once again if you give some program & explain all the annotation in this article.

    Thanks for sharing this.

  32. Anonymous says:

    Good Explanation Thank you sir

  33. ln says:

    What is the exact purpose of @Component

  34. Anonymous says:

    if u understand everything in this post please explain me @Component annotation

    Thanks in advance

  35. Joseph says:

    Thank you joe it was really great to have your website.

  36. venkat says:

    can you explain clearly with spring annotations

  37. Karthik says:

    Loved your explanation

  38. Manik says:

    Good explanation, it cleared all my doubts

  39. SEKHAR says:

    GOOD EXPLANATION

  40. Santiago says:

    @Controller has a different default scope = prototype while the others have singleton.

  41. Anonymous says:

    nice explaination

  42. Nitish says:

    nice one

Comments are closed for this "Spring @Component, @Service, @Repository, @Controller Difference" tutorial.