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.
The postprocessor automatically looks for all exception translators (implementations of the PersistenceExceptionTranslator interface) and advises all beans marked with the @Repository annotation so that the discovered translators can intercept and apply the appropriate translation on the thrown exceptions.
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>
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>
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>
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.
Comments are closed for "Spring @Component, @Service, @Repository, @Controller Difference".
Good One!
Excellent way of explaination.. Too good
Thanks Joe,you explain concepts very well.I enjoy read those and also I am learning many things.
Good one.
but when are these beans instantiated
Nice article ,but 1 query,when does spring container instantiate the bean?
Nicely Explained
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.
its very clear and nicely explined.
Thanks a lot.
good…
good guid
Very good article .
Very good one.
Excellent Joe!
Excellent succinct overview…Thanks
not clear as it does not give more detils
Good job!
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
superb explaination boss… thank u
very informative
New to SPRING…This is great..GOOD expalnation…KEEP IT UP (y)
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..
Nice way to explain.. it fills the knowledge gap,,,
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,
Couldnt understand images
how can I config a bean with more than one name using annotation like xml.
for xml example:
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 .
Its really help me to get clear idea about core annotations
Thanks a lot
Good One!
Thank you!!
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.
Good Explanation Thank you sir
What is the exact purpose of @Component
if u understand everything in this post please explain me @Component annotation
Thanks in advance
Thank you joe it was really great to have your website.
can you explain clearly with spring annotations
Loved your explanation
Good explanation, it cleared all my doubts
GOOD EXPLANATION
@Controller has a different default scope = prototype while the others have singleton.
nice explaination
nice one