Spring Boot - Annotations
Last Updated :
02 Jan, 2025
Spring Boot Annotations are a form of metadata that provides data about a spring application. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Following are some of the features of Spring Boot:
- It allows for avoiding heavy configuration of XML which is present in the spring
- It provides easy maintenance and creation of REST endpoints
- It includes embedded Tomcat-server
- Deployment is very easy, war and jar files can be easily deployed in the Tomcat server
Spring Boot Annotations
Spring annotations are present in the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages are commonly known as Spring Boot annotations.
Spring Boot Annotations List
Some of the annotations that are available in this category are:
- @SpringBootApplication
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
- Auto-Configuration Conditions
- @ConditionalOnClass, and @ConditionalOnMissingClass
- @ConditionalOnBean, and @ConditionalOnMissingBean
- @ConditionalOnProperty
- @ConditionalOnResource
- @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
- @ConditionalExpression
- @Conditional
1. @SpringBootApplication Annotation
This annotation is used to mark the main class of a Spring Boot application. It encapsulates @SpringBootConfiguration , @EnableAutoConfiguration , and @ComponentScan annotations with their default attributes.

Example:
Java
@SpringBootApplication
// Class
public class DemoApplication {
// Main driver method
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
2. @SpringBootConfiguration Annotation
It is a class-level annotation that is part of the Spring Boot framework. It implies that a class provides Spring Boot application configuration. It can be used as an alternative to Spring’s standard @Configuration annotation so that configuration can be found automatically. Most Spring Boot Applications use @SpringBootConfiguration via @SpringBootApplication. If an application uses @SpringBootApplication, it is already using @SpringBootConfiguration.
Example:
Java
@SpringBootConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public StudentService studentService() {
return new StudentServiceImpl();
}
}
3. @EnableAutoConfiguration Annotation
This annotation auto-configures the beans that are present in the classpath. It simplifies the developer's work by assuming the required beans from the classpath and configure it to run the application. This annotation is part of the spring boot framework. For example, when we illustrate the spring-boot-starter-web dependency in the classpath, Spring boot auto-configures Tomcat , and Spring MVC . The package of the class declaring the @EnableAutoConfiguration annotation is considered as the default. Therefore, we need to apply the @EnableAutoConfiguration annotation in the root package so that every sub-packages and class can be examined.
Example:
Java
@Configuration
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4. @ComponentScan Annotation
@ComponentScan tells Spring in which packages you have annotated classes that should be managed by Spring. So, for example, if you have a class annotated with @Controller which is in a package that is not scanned by Spring, you will not be able to use it as a Spring controller. So we can say @ComponentScan enables Spring to scan for things like configurations, controllers, services, and other components that are defined. Generally, @ComponentScan annotation is used with @Configuration annotation to specify the package for Spring to scan for components.
Example:
Java
@Configuration
@ComponentScan
// Main class
public class Application {
// Main driver method
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
5. @ConditionalOnClass Annotation and @ConditionalOnMissingClass Annotation
@ConditionalOnClass Annotation used to mark auto-configuration bean if the class in the annotation's argument is present/absent.
Example:
Java
@Configuration
@ConditionalOnClass(MongoDBService.class)
class MongoDBConfiguration {
// Insert code here
}
6. @ConditionalOnBean Annotation and @ConditionalOnMissingBean Annotation
These annotations are used to let a bean be included based on the presence or absence of specific beans.
Example:
Java
@Bean
@ConditionalOnMissingBean(type = "JpaTransactionManager")
JpaTransactionManager jpaTransactionManager(
EntityManagerFactory entityManagerFactory)
{
// Insert code here
}
7. @ConditionalOnProperty Annotation
These annotations are used to let configuration be included based on the presence and value of a Spring Environment property.
Example:
Java
@Bean
@ConditionalOnProperty(name = "usemongodb",
havingValue = "local")
DataSource
dataSource()
{
// Insert code here
}
@Bean
@ConditionalOnProperty(name = "usemongodb",
havingValue = "prod")
DataSource
dataSource()
{
// Insert code here
}
8. @ConditionalOnResource Annotation
These annotations are used to let configuration be included only when a specific resource is present in the classpath.
Example:
Java
@ConditionalOnResource(resources = "classpath:mongodb.properties")
Properties
additionalProperties()
{
// Insert code here
}
9. @ConditionalOnExpression Annotation
These annotations are used to let configuration be included based on the result of a SpEL (Spring Expression Language) expression.
SpEL (Spring Expression Language): It is a powerful expression language that supports querying and manipulating an object graph at runtime.
Example:
Java
@Bean
@ConditionalOnExpression("${env} && ${havingValue == 'local'}")
DataSource dataSource()
{
// Insert code here
}
10. @ConditionalOnCloudPlatform Annotation
These annotations are used to let configuration be included when the specified cloud platform is active.
Example:
Java
@Configuration
@ConditionalOnCloudPlatform(CloudPlatform.CLOUD_FOUNDRY)
public class CloudConfigurationExample
{
// Insert code here
}
Request Handling and Controller annotations:
Some important annotations comes under this category are:
- @Controller
- @RestController
- @RequestMapping
- @RequestParam
- @PathVariable
- @RequestBody
- @ResponseBody
- @ModelAttribute
1. @Controller Annotation
This annotation provides Spring MVC features. It is used to create Controller classes and simultaneously it handles the HTTP requests. Generally we use @Controller annotation with @RequestMapping annotation to map HTTP requests with methods inside a controller class.
Example:
Java
//Create a Java class and use @Controller annotation to make it controller class
@Controller
public class MyController{
public String GFG(){
//insert code here
}
}
2. @RestController Annotation
This annotation is used to handle REST APIs such as GET, PUT, POST, DELETE etc. and also used to create RESTful web services using Spring MVC.
It encapsulates @Controller annotation and @ResponseBody annotation with their default attributes.
@RestController = @Controller + @ResponseBody
Example:
Java
//Create a Java class and use @RestController annotation
// to make the class as a request handler
@RestController
public class HelloController{
public String GFG(){
//insert code here
}
}
3. @RequestMapping Annotation
This annotation is used to map the HTTP requests with the handler methods inside the controller class.
Example:
Java
//Java program to demonstrate @RequestMapping annotation
@RestController
public class MyController{
@RequestMapping(value=" ",method=RequestMapping.GET)
public String GFG(){
//insert code here
}
}
For handling specific HTTP requests we can use
- @GetMapping
- @PutMapping
- @PostMapping
- @PatchMapping
- @DeleteMapping
NOTE : We can manually use GET, POST, PUT and DELETE annotations along with the path as well as we can use @RequestMapping annotation along with the method for all the above handler requests
4. @RequestParam Annotation
This annotation is basically used to obtain a parameter from URI. In other words, we can say that @RequestParam annotation is used to read the form data and binds the web request parameter to a specific controller method.
Example:
Java
//Java code to demonstrate @RequestParam annotation
@RestController
public class MyController{
@GetMapping("/authors")
public String getAuthors(@RequestParam(name="authorName") String name){
//insert code here
}
}
5. @PathVariable Annotation
This annotation is used to extract the data from the URI path. It binds the URL template path variable with method variable.
Example:
Java
//Java Program to Demonstrate @PathVariable annotation
@RestController
public class MyController{
@GetMapping("/author/{authorName}")
public String getAuthorName(@PathVariable(name = "authorName") String name){
//insert your code here
}
}
6. @RequestBody Annotation
This annotation is used to convert HTTP requests from incoming JSON format to domain objects directly from request body. Here method parameter binds with the body of the HTTP request.
Example:
Java
// Java Code to Demonstrate @RequestBody annotation
@RestController
public class MyController{
@GetMapping("/author")
public void printAuthor(@RequestBody Author author){
//insert code here
}
}
7. @ResponseBody Annotation
This annotation is used to convert the domain object into HTTP request in the form of JSON or any other text. Here, the return type of the method binds with the HTTP response body.
Java
//Java code to demonstrate @ResponseBody annotation
@Controller
public class MyController{
public @ResponseBody Author getAuthor(){
Author author = new Author();
author.setName("GFG");
author.setAge(20);
return author;
}
}
8. @ModelAttribute Annotation
This annotation refers to model object in Spring MVC. It can be used on methods or method arguments as well.
Example:
@ModelAttribute("author")
public Author author(){
//insert code here
}
Here, we don't need to add object explicitly to the model, because automatically object will be the part of the attribute.
Similar Reads
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Introduction to Spring Boot
Spring is widely used for creating scalable applications. For web applications, Spring provides Spring MVC, a commonly used module for building robust web applications. The major drawback of traditional Spring projects is that configuration can be time-consuming and overwhelming for new developers.
5 min read
Best Way to Master Spring Boot â A Complete Roadmap
In the corporate world, they say "Java is immortal!". But Why? Java remains one of the major platforms for developing enterprise applications. Enterprise Applications are used by large companies to make money. Those applications have high-reliability requirements and an enormous codebase. According
14 min read
How to Create a Spring Boot Project?
Spring Boot is built on top of the spring and contains all the features of spring. It is one of the most popular frameworks for building Java-based web applications and microservices. It is a favorite among developers due to its rapid, production-ready environment, which allows developers to focus o
6 min read
Spring Boot - Annotations
Spring Boot Annotations are a form of metadata that provides data about a spring application. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the
7 min read
Spring Boot - Architecture
Spring Boot is built on top of the core Spring framework. It simplifies and automates Spring-based application development by reducing the need for manual configuration. Spring Boot follows a layered architecture, where each layer interacts with other layers in a hierarchical order. The official Spr
3 min read
Spring Boot Actuator
Developing and managing an application are the two most important aspects of the applicationâs life cycle. It is very important to know what is going on beneath the application. Also, when we push the application into production, managing it gradually becomes critically important. Therefore, it is a
5 min read
Spring Boot - Introduction to RESTful Web Services
RESTful Web Services REST stands for REpresentational State Transfer. It was developed by Roy Thomas Fielding, one of the principal authors of the web protocol HTTP. Consequently, REST was an architectural approach designed to make the optimum use of the HTTP protocol. It uses the concepts and verbs
5 min read
How to create a basic application in Java Spring Boot
Spring Boot is the most popular Java framework that is used for developing RESTful web applications. In this article, we will see how to create a basic Spring Boot application.Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also p
3 min read
How to Create a REST API using Java Spring Boot?
Representational State Transfer (REST) is a software architectural style that defines a set of constraints for creating web services. RESTful web services allow systems to access and manipulate web resources through a uniform and predefined set of stateless operations. Unlike SOAP, which exposes its
4 min read