In the previous
chapters, we have seen how we can use configure the dependencies using XML.
XML configuration
can be heavy and increase the amount of code that we need to write to make the
configurations. Since spring 2.5 it became possible to configure the spring
dependency injection using annotations.
This chapter will
cover:
1.
What
are Spring annotations?
2.
How
to turn on annotations in spring?
3.
Some
Important annotations in Spring Framework.
What are
spring annotations?
Before spring 2.5, to define any dependency injection in the framework we
use XML based configuration. These XML based configurations are very difficult
to write and it increases the amount of code that we need to write.
Spring 2.5 and higher version provides the facility of declaring the
dependencies by using annotation configuration. These annotations based
configuration is very easy to use and reduce the amount of code that we need to
implement to define the configuration.
How to turn
on annotation configuration in spring?
The annotation based configuration is not turned on by default in spring.
We need to make some configuration to turn the spring annotation on.
To turn on spring annotation based configuration we use <context:annotaition-config/> tag in spring configuration file. Below is the example for the same:
After <context:annotation-config/> is used we can use the
annotations that are provided in the spring framework.
Some important
spring annotations:
There are lots of annotations that are provided by the spring framework. But,
in this chapter, we will see some of the most important annotations that we come
across in daily coding.
1.
@Autowired:
Usually while
using XML based configuration we specify bean that will be used in another bean
use ref attribute. But spring framework supports @Autowired annotation to
do the same thing without XML configuration.
There are a total
of 6 ways by which we can autowire a spring bean:
1.
Autowire
byName –
These types of autowiring are used on setter methods. The
variable name in spring configuration file and class where we are defining the
autowiring should be same.
2.
Autowire
byType –
For this type of autowiring class, types are used.
3.
Autowire
by constructor –
This is similar to autowire byType only difference is,
in this constructors are used for autowiring.
4.
Autowire
by autodetect –
This type of autowire is available with spring 3.0 or
order. This is a deprecated autowire type.
5.
@Autowired
annotation –
We can use @Autowired annotation to autowire a spring bean. This annotation can be used on methods and variables. This will be autowire byType. We can also use it on constructor for constructor based autowiring.
6.
@Qualifier
annotation –
This annotation can be used when we have ambiguity exception. @Qualifier annotation can be used to resolve conflicts in bean mapping.
Let us understand
@Autowire annotation with an example. Let us take below Employee.java as the model
bean.
Employee.java
Using @Autowire for autowiring byType
2.
@Required
–
This annotation is
used on the bean property setter method and it indicates that the affected bean
property must be populated in XML configuration file at the time of
configuration. The example of @Required annotation is given below:
In the above example, we can see that the @Required annotation is used on name property which means that when the Employee bean is configured it should have a default value for “name” property.
If in case we fail
to give the value while configuring the bean the compiler will throw BeanInitializationException.
3.
@Qualifier
–
@Qualifier will
resolve the ambiguity exception. Let us see an example without the @Qualifier
annotation and what is the exact problem that can occur.
Consider three
class TextFormater.java, PDFFormatter.java and FormatterService.java. Content
of which are given below:
TextFormatter.java
PDFFormatter.java
FormatterService.java
Now, if we try to
load FormatterService.java into the context then the compiler will throw an
exception NoUniqueBeanDefinitionException. This is because bean does
not know which bean to inject. To resolve NoUniqueBeanDefinitionException
we use @Qualifier annotation as below:
TextFormatter.java
PDFFormatter.java
FormatterService.java
With above example
we can see that in FormatterService.java if we use @Qualifier we are specifying
the Formatter type which we want to autowire.
These were some of
the important annotations that we come across every day. Some other annotations
are:
1.
@Component
– this annotation is
used to mark the bean as the component which will be managed by spring. @Service,
@Repository is the special case of @Component.
2.
@Service
– this annotation is
used on the class to indicate that this class is used at the service layer and
holds business logic.
3.
@Repository
– this annotation
is used at the class level to indicate that the class is used at the repository
layer. Any persistence specific exception will be handled by this class.
4.
@Controller
– this annotation
will mark the class as spring controller and all the request and response will
be handled by this class.
So, with the use
of spring annotation based configuration, we eliminate the use of XML based
configurations which provides more flexibility to the application.
That is all for
this chapter. Hope you like the article.
The Next Chapter
in the series will be Spring – Dependency Injection.
1 Comments
nice
ReplyDelete