So, far we were dealing with simple beans which are easy to write in 'application-context.xml'. But spring is meant to deal with large applications. Where the business logic is written in a Service layer(Which is actually a class) and the database related things are written in DAO or Data Access Object layer(Which is also a class).
Till now we were dealing with @Component annotation but Spring also provides two more annotations @Service and @Repository. Although, they works exactly same as @Component but it makes more sense if we are using @Service for a Service class and @Repository for a DAO class. They are known as Stereotype Annotations.
As we have seen, using annotations we do not have to rewrite the bean using the <bean..> tag in 'application-context.xml'. Rather annotate the class itself so that it acts like a bean.
To demonstrate the example we will be writing the code without Annotations first and then write the code with annotations and compare both.
So, let us define a DAO(the class which interacts with database) class :
So, we have a 'searchCarBrand()' inside the 'SpringDAO' class which returns a 'Car' object. Since, we are not dealing with database, we have hardcoded the 'brand' property of 'Car' to Honda.
Now, let us define the service class. Ideally Service class should call the DAO class.
So, in 'SpringService' we have defined a method 'searchCar()' which is going to return the 'Car' object from the 'SpringDAO' class.
For simplicity just remember 'SpringService' is the Service class which contains a reference of the DAO class 'SpringDAO'.
Then let us define the 'application-context.xml' :
In the above code we have created a bean 'springDAORef'
with empty body as it doesn't have any property. It only has a method 'searchCarBrand()', which gets initialized when the bean is created.
Then injected the bean 'springDAORef' to 'SpringService':
Now, let us try the above example with annotations and get rid of the <bean..> definition tags in 'application-context.xml'.
@Repository annotation tells Spring that a 'SpringDAO' bean has to be created while spring starts. We could have used @Component instead but makes more sense if a DAO class is marked with @Repository.
Similarly, @Service annotation tells Spring that a 'SpringService' bean has to be created while spring starts. Although, we could have used @Component instead but makes more sense if a Service class is marked with @Service.
Finally, in application-context.xml we need to tell spring that we are using annotations and also say in which package spring needs to look for those annotations.
In the above xml we have added a few more namespaces for supporting annotation. You can copy the same and paste in your 'application-context.xml'.
Next, we are telling spring that the application has annotations.
Also, inform spring that in which package it needs to scan for the annotated classes.
Assuming our annotated classes are in 'com.learnerslesson' package
Finally, in the main() method we define our application code to interact with spring.