menu

Spring Boot Annotations

1. @PathVariable vs @RequestParam

@PathVariable : The data is sent as a part of the URL

Ex: localhost:8080/pathvariabledemo/1/Rohit

@RequestParam: The data is sent in query param.

Ex: localhost:8080/requestparamdemo?id=1&name=Rohit

2. @ResponseBody:

When we use @ResponseBody annotation, then Spring convert the return value into HTTP Response Object automatically. It means, If we want to return anything in HTTP response, then we need to use this annotation.

3. @Controller:

This annotation was introduce in Spring 2.5 version and it is used to create a web controller class, which deals with the web request and send HTTP response for RESTful APIs with the help of @ResponseBody annotation.

4. @RestController:

This annotation was introduced in Spring 4.0 version . It is a combination of two annotations @Controller and @ResponseBody.

5. @Component:

It is a main stereotype annotation. Stereotype annotatios are used to convert a simple class into a spring bean class. A spring bean class are those classes which are managed by spring framework. Spring has the responsibility for object creation, Object Autowiring and Object destruction.

There are some more stereotype annotations which are derived from @Component annotation.

@Controller

@Service

@Repository

image not found

From the above block diagram. Let us assume Functionality X is the capability used for convert a simple java class into a spring bean class.

@Controller:

This annotation indicates that it is a spring bean class as well as it is a web controller class also. A web controller class is a class which can accept a web request and sends a HTTP response to the web.

@Service:

This annotation is used for service class, in which we can perform a business logic operation. This class is a simple spring bean class.

@Repository:

This annotation gives the capability to a class to become a spring bean class, as well as this annotation having some extra capability also that is to convert a JDBC exception into a spring unchecked (runtime) exception.

6. @Autowired:

In spring framework dependency injection is performed by using this @Autowired annotation. It binds a class or bean object with its appropriate refrence variable at run time.

There are many ways to achieve autowiring in spring:

byType:

This binds the class Object with the refrence variable by using the bean or class type. This is the default functionality of spring framework that it always binds a refrence variable with its matching class Object only.

image not found

byname:

In this case it uses the name of the refrence variable to bind the object and resolve the conflict.

image not found

@Qualifier:

We can use this annotation to resolved the ambiguity which occurs during object binding. Let’s say in heap memory two objects are available which are eligible to bind with a single refrence variable, by using @Qualifier annotation we can resolved this ambiguity problem.

image not found

@Primary:

If you want to give one implementation priority over all others implememtations, then you can use this annotation.

image not found

7. @SpringBootApplication:

This annotation is used at the root class of our spring boot application, which containing the main method. We can find this root class inside our base package of application.

This annotation is used to perform all auto configuration and component scan(managing spring bean) in our spring boot application, basically this annotation is a combination of these 3 annotations @EnableAutoConfiguration, @SpringBootConfiguration, @ComponentScan.

@EnableAutoConfiguration:

It does auto configuration based on the framework or classes available in classpath (pom.xml file). Suppose, if you have used spring-boot-starter-web dependency in classpath then Spring Boot auto-configure tomcat server and Spring MVC for your spring boot project.

@SpringBootConfiguration:

It is just the enhancement of @Configuration annotation of spring MVC framework, it is used to configure application context for our spring boot application.

@ComponentScan:

It performs the scanning of all classes available in base package and its hierarchy. If it found any class annotated with @Component or its child annotations then it creates the object of those classes and manage the life cycle.

8. @ConfigurationProperties:

This annotation is used to map the content of .properties file to a spring bean variables. A property file can be either .properties file or .yaml file.