Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring - Stereotype Annotations

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).


Stereotype Annotations

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.


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.



Example without using Annotations

So, let us define a DAO(the class which interacts with database) class :


class SpringDAO {

 public Car searchCarBrand() {

   Car car = new Car();
   car.setBrand("Honda");

   return car;
 }
}


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.


class SpringService {

 SpringDAO springDAO = new SpringDAO();

 public Car searchCar() {

   Car car = springDAO.searchCarBrand();
   return car;
 }
}

So, in 'SpringService' we have defined a method 'searchCar()' which is going to return the 'Car' object from the 'SpringDAO' class.


Car car = springDAO.searchCarBrand();

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' :


application-context.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
 xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation = "http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean name = "springService" class = "SpringService">
   <property name = "springDAO" ref="springDAORef"/>
 </bean>

 <bean name = "springDAORef" class = "SpringDAO">

 </bean>

 </beans>

In the above code we have created a bean 'springDAORef'


<bean name = "springDAORef" class = "SpringDAO">

</bean>

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':


<property name = "springDAO" ref="springDAORef"/>

Example with using Annotations

Now, let us try the above example with annotations and get rid of the <bean..> definition tags in 'application-context.xml'.


The DAO class

import org.springframework.stereotype.Repository;

@Repository
class SpringDAO {

 public Car searchCarBrand() {

   Car car = new Car();
   car.setBrand("Honda");

   return car;
 }
}

@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.


The Service class

import org.springframework.stereotype.Component;

@Service
class SpringService {

 SpringDAO springDAO = new SpringDAO();

 public Car searchCar() {

   Car car = springDAO.searchCarBrand();
   return car;
 }
}

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.


application-context.xml

<?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-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:annotation-config/>

  <context:component-scan base-package="com.learnerslesson" />


</beans >

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.


<context:annotation-config/>

Also, inform spring that in which package it needs to scan for the annotated classes.


<context:component-scan base-package="com.learnerslesson" />

Assuming our annotated classes are in 'com.learnerslesson' package


Finally, in the main() method we define our application code to interact with spring.


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MySpringApp {
  public static void main(String[] args) {

  ApplicationContext applicationContext =
            new ClassPathXmlApplicationContext("application-context.xml");

  SpringService springService = (SpringService)applicationContext.getBean("springService");

  System.out.println("The Car brand is : "+springService.searchCar());

  }
}