In spring autowiring, @Autowired
annotation handles only wiring part. We still have to define the beans so the container is aware of them and can inject them for us.
With @Component
, @Repository
, @Service
and @Controller
annotations in place and automatic component scanning enabled, Spring will automatically import the beans into the container and inject to dependencies. These annotations are called Stereotype annotations as well.
Before jumping to example use of these annotations, let’s learn quick facts about these annotations which will help us in making a better decision about when to use which annotation.
Table of contents 1. Stereotype annotations 2. Enable component scanning 3. How to use stereotype annotations 4. Difference between @Component and @Bean 5. Demo
1. Spring bean stereotype annotations
1.1. @Component annotation
The @Component
annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
@Component public class EmployeeDAOImpl implements EmployeeDAO { ... }
1.2. @Repository annotation
Although above use of @Component
is good enough but we can use more suitable annotation that provides additional benefits specifically for DAOs i.e. @Repository
annotation. The @Repository
annotation is a specialization of the @Component
annotation with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException
.
1.3. @Service annotation
The @Service
annotation is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component
annotation, but it’s a good idea to use @Service
over @Component
in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.
1.4. @Controller annotation
@Controller
annotation marks a class as a Spring Web MVC controller. It too is a @Component
specialization, so beans marked with it are automatically imported into the DI container. When we add the @Controller
annotation to a class, we can use another annotation i.e. @RequestMapping
; to map URLs to instance methods of a class.
@Component
annotation. Most of the time, we will using @Repository
, @Service
and @Controller
annotations. @Component
should be used when the class does not fall into either of three categories i.e. controller, manager and dao.If we want to define name of the bean with which they will be registered in DI container, we can pass the name in annotation attribute itself e.g. @Service (“employeeManager”).
2. Enable component scanning
Above four annotations will be scanned and configured only when they are scanned by DI container of Spring framework. To enable this scanning, we will need to use “context:component-scan” tag in our applicationContext.xml
file.
<context:component-scan base-package="com.howtodoinjava.demo.service" /> <context:component-scan base-package="com.howtodoinjava.demo.dao" /> <context:component-scan base-package="com.howtodoinjava.demo.controller" />
The context:component-scan element requires a base-package
attribute, which, as its name suggests, specifies a starting point for a recursive component search. We may not want to give top package for scanning to spring, so you should declare three component-scan elements, each with a base-package attribute pointing to a different package.
3. Using @Component, @Repository, @Service and @Controller annotations
As I already said that you use @Repository
, @Service
and @Controller
annotations over DAO, manager and controller classes. But in real life, at DAO and manager layer we often have separate classes and interfaces. Interface for defining the contract, and classes for defining the implementations of contracts.
Where to use these annotations? Let’s find out.
Always use the annotations over concrete classes; not over interfaces.
public interface EmployeeDAO { //... } @Repository public class EmployeeDAOImpl implements EmployeeDAO { //... }
Once you have these stereotype annotations on beans, you can directly use bean references defined inside concrete classes. Note the references are of type interfaces. Spring DI container is smart enough to inject the correct instance in this case.
4. Difference between @Component and @Bean annotations
In Spring, both annotations are quite different.
@Component
used to auto-detect and auto-configure beans using classpath scanning. There’s an implicit one-to-one mapping between the annotated class and the bean (i.e. one bean per class).
@Bean
is used to explicitly declare a single bean, rather than letting Spring do it automatically for us.
Another big difference is that @Component
is a class level annotation where as @Bean
is a method level annotation and ,by default, name of the method serves as the bean name.
5. Demo
5.1. Bean definitions
public interface EmployeeDAO { public EmployeeDTO createNewEmployee(); } @Repository ("employeeDao") public class EmployeeDAOImpl implements EmployeeDAO { public EmployeeDTO createNewEmployee() { EmployeeDTO e = new EmployeeDTO(); e.setId(1); e.setFirstName("Lokesh"); e.setLastName("Gupta"); return e; } }
public interface EmployeeManager { public EmployeeDTO createNewEmployee(); } @Service ("employeeManager") public class EmployeeManagerImpl implements EmployeeManager { @Autowired EmployeeDAO dao; public EmployeeDTO createNewEmployee() { return dao.createNewEmployee(); } }
@Controller ("employeeController") public class EmployeeController { @Autowired EmployeeManager manager; public EmployeeDTO createNewEmployee() { return manager.createNewEmployee(); } }
public class EmployeeDTO { private Integer id; private String firstName; private String lastName; }
5.2. Run the demo
Let’s test the above configuration and annotations:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.howtodoinjava.demo.service.EmployeeManager; public class TestSpringContext { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //EmployeeManager manager = (EmployeeManager) context.getBean(EmployeeManager.class); //OR this will also work EmployeeController controller = (EmployeeController) context.getBean("employeeController"); System.out.println(controller.createNewEmployee()); } }
Program Output.
Jan 22, 2015 6:17:57 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1b2b2f7f: startup date [Thu Jan 22 18:17:57 IST 2015]; root of context hierarchy Jan 22, 2015 6:17:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] Employee [id=1, firstName=Lokesh, lastName=Gupta]
Drop me a comment/query if something needs more explanation.
Happy Learning !!
Not found any satisfied explanation for @Bean vs @Component, anywhere. Can you explain in more detail?
Please refer to section 4. I will add more detail in future.
if we wont use these sterotype annotation then also i am getting the result, then how these stereotype annotation helpful for the java class?
Very good article,Can you please add some more annotations.
Thanks nice post
simple and good explanation.. thanks
helpful
Nice post Lokesh thanks
Nice Post Lokesh 🙂 keep it up 🙂
Will you explain briefly on what the flow of application from xml to classes in step by step using annotations.
Thank you.
CAN U PLEASE ADD SPRING JDBC TUTORIAL ALSO
I will try to find time.
Very Nice Article!
Perfect article for me as a beginner. Thank you really much!
what do you meant by unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
What is the necessary to translate the unchecked exceptions to DataAccessException
Very thorough tutorial. Thanks.
Nice article. Thanks!
Why didnt you put the full xml file? Only those three lines didnt help.
as long as the xml file is in the resources directory and its in your classpath you should not have nay issues. Hope that helps!
Perfect post.
Hi Lokesh,
Whenever i want to learn some concepts in simple way i search on google which opens lot of links and always search for your link to open.
It’s due to the simplicity that you have maintained across all tutorials. Simply loved with these tutorial.
Waiting for more tutorials.. Best luck.
Thanks,
Sunil
Thanks for the motivation.
Hi,
I have doubt with application-context.xml, if you are using annotations to wire then why we should use the context.getBean () to wire.
Can you explain the flow I mean how controller class autowire employeemanager class after that employeeDAO class.
Is it autowired by type or what? I am little bit confuse here.
kindly, expalin.
Can u explain @Component annotation in detail actually i used this in mapper layer but want more explain of this layer also
Is the intention of Spring to have an Interface for every @Component?
Probably, it’s more evident from sourcecode of spring framework itself. Anyway, I like “design by interface” approach. Your thoughts?
Thanks for the article.
Can you guide me how to make it available service level classes to controller. In this example you simply declared @Autowired annotation for service bean in controller. But in real scenario(MVC) both the layers of service and controller are in two separate containers. How make service beans available to controller.
What you mean by “two separate containers”? Different packages – in that case, it will work. Different spring context – NO, it will not work.
Why did You inject the interface , not concrete class ? e.g. EmployeeDAO dao is injected in EmployeeManager
why don’t you inject the concrete class EmployeeDAOImpl ? it’s more reasonable and rational to do that
i injected the concrete class and it gives me error
Because there can be many implementations of
EmployeeDAO
when you have multiple datasources. You may inject any implementation to interface reference, but using concrete class would not allow you to change implementations. It’s just an example.Overall, it promotes “design by interface” which I also admire and use everywhere.
If I have Two implementation of an interface, and I want a particular implementation to be autowired, how can I do that ?? Also the names that you passed in the annotation e.g. @Repository (“employeeDao”), has any relation with the interface name? or is it just a random string
In this case, you will use
@Qualifier("employeeDao")
annotation to clearly specify which implementation shall be autowired.Very good Article.It is very easy to understand and it helped me a lot.
Thank you Lokesh 🙂
A really good article. Something worth mentioning is that there is one exception to the interchangeability of these annotations. The Repository annotation confers special behavior to all beans it marks. The PersistenceExceptionTranslationPostProcessor automatically applies persistence exception translation to any bean marked with @Repository. I have post more about this here: https://readlearncode.com/spring-4/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1.
Nice explanation thanks it help me lots.
Hi,
Firstly, your blog is amazing! The articles are explained in such simple language with such detailed examples. I’ve been following your blog for a year now and it has definitely made me a better coder 🙂 Keep up the good work!
Coming to the point, I have a doubt here. What if for one EmployeeDao interface, there are 2 concrete classes implementing it (isn’t that the whole idea behind an interface?). How will the service layer understand which daoImpl to call?
I can relate to above situation only when application interacts with two kind of data stores (e.g. excel and mysql), and the implementation classes have all together different logic for each method declared in interface. In that case – you will be selecting appropriate implementation based on some condition. You may take help of abstractroutingdatasource.
Apart from above condition, I do not think why there will any other reason for two DAO implementations. I hope, I make sense.
Hi Lokesh,
I understand very easily.
Thanks for providing online guide in simple manner.
my suggestion is , can you please post the architecture of the project which you explained above.
-Thank you
Amazing, thanks!
Very nice explanation. Easy to understand.
Thank you !!
very much thankful to you . As beginner to spring this post really helped a lot
Great blog!
Thank you very much, Lokesh
can I use @repository for a Service class??As all are @component type and just serve as marker to a class can we use these interchangeably?
I will suggest to use @Repository for DAO classes and @Service for service class. Though they currently do not provide any major functionality benefit, but in future releases they may provide. In addition, they make code more readable and help identifying classes correctly – for their responsibilities.
Great Article. The reading is very simple although my english isn’t good – I like that.
This article really help me a lot!
So appreciated!
Nice tutorial with good explanation…
Thank you sir.. Very easy to understand.. I could resist commenting 🙂
Nice post very simple and quite understandable with good example.
Thanks for very much.
Useful sample, excellent explanation! thank.
How to download the code?
Thanks
Is there any use of EmployeeController class ?
In the tutorial you are directly accessing bean of service level and calling dao, where does the controller come into picture ?
Shouldn’t EmployeeManager manager be marked with @Autowired in EmployeeController.class ?
Great articles, love your teaching through simple examples way!
Yeh ! It should be. I forgot probably because I directly used manager class from
TestSpringContext
. I will correct this one.Very helpful post. Will be my reference when I will be training my colleagues for Spring MVC components/annotations. Thanks!
Perhaps @RestController annotation should also be mentioned for people using newer Spring versions. Awesome tutorial nevertheless!
howtodoinjava always gives simplified tutorials / explainations.
Really helps & Easy to understand.
Always thankful to Lokesh.
Thanks Kalpesh.. 🙂