In this tutorial, we will learn to implement Bean Autowiring and provide relevant dependencies. We will also learn to write the correct configuration which will be annotation based.
As already stated in our previous articles of the series, Spring framework is an open source Java platform that provides MVC infrastructure support for developing robust Java applications very easily and very rapidly using recommended MVC pattern.
How does Autowiring happens?
Now that we have learned to define our beans, Spring container can autowire them for us and manage them as well.
First, and most important—all Spring beans are managed. They “live” inside a container called the “application context.”
Second, each application has an entry point to that context. Web applications have a Servlet, JSF uses a el-resolver, etc. Also, there is a place where the application context is bootstrapped and all beans autowired. In web applications this can be a startup listener.
Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.
What is “living” in the application context? This means that the context instantiates the objects, not you. i.e. you never make new UserServiceImpl(). The container finds each injection point and sets an instance there.
In your controllers, you just have the following:
A few things to take care of:
The @Autowired annotation allows us to skip configurations elsewhere of what to inject and just does it for you. Assuming the package is com.discoversdk.blogs you have to put this tag in your XML (application context file):
This tag will do auto-scanning. Assuming each class that has to become a bean is annotated with a correct annotation like @Component (for a simple bean) or @Controller (for a servlet control) or @Repository (for DAO classes) and these classes are somewhere under the package com.discoversdk.blogs, Spring will find all of these and create a bean for each one.
This is done in 2 scans of the classes:
In the second scan, Spring searches for a bean of type BlogFinder, and if it finds such a bean, it injects it into this method. If it finds two such beans you will get an exception. To avoid the exception, we can use the @Qualifier annotation and tell it which of the two beans to inject in the following manner:
Or if you prefer to declare the beans in your XML, it would look something like this:
In the @Autowired declaration, we need to also add the @Qualifier to tell which of the two color beans to inject:
If we want to avoid two annotations (the @Autowired and @Qualifier) we can use @Resource to combine these two:
The @Resource spares us the use of two annotations and instead we only use one.
So, to sum it up: The @Autowired annotation spares us the need to do the wiring by ourself in the XML file (or any other way) and just finds for us what needs to be injected where, and does that for us.
Disadvantages of Autowiring
There are not too many really, but there are some things to watch out for with Autowiring. Let’s quickly look at two of the disadvantages Autowiring presents:
There are basically 5 modes in which Autowiring can be performed. These are:
In this article, we learned about how to inject and autowire beans in Spring. We also had a look at what disadvantages wiring has. So let us know if you have any luck with autowire in Spring or have any questions. Leave a comment below!
By Subham Aggarwal |
This content was originally published here.