Spring Boot Important Annotations
Spring Boot Important Annotations
Web Development
Software Development
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 1/23
14/04/2024, 19:29 Spring Boot Important Annotations
3. @Configuration
8. @Value
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 2/23
14/04/2024, 19:29 Spring Boot Important Annotations
21. @PatchMapping
Content Management
27. @RestController
Engineering
28. @RequestAttribute
Soft Skills
29. @CookieValue
See All
30. @CrossOrigin
The Support for annotations in Java is provided since Java 5. leading Java
frameworks quickly adopt the annotation’s concept. Spring Boot has provided the
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 3/23
14/04/2024, 19:29 Spring Boot Important Annotations
Due to their extensive structure and internal concepts, the annotations provide a lot of
Before the annotations, the Spring Boot project's behavior was controlled and
org.springframework.boot.autoconfigure and
org.springframework.boot.autoconfigure.condition packages.
Let’s discuss some frequently used spring boot annotations with examples:
@Required
The @Required annotation is applied on the setter method of the bean file. Use of
this annotation indicates that the annotated bean must be populated at configuration
BeanInitializationException.
package com.anjum.SpringBootDemo;
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 4/23
14/04/2024, 19:29 Spring Boot Important Annotations
import org.springframework.beans.factory.annotation.Required;
return id;
@Required
this.id = id;
@Autowired
and constructors. It provides auto wiring to bean properties. When we apply the
@autowire to a field, Spring contain will automatically assign the fields with
assigned values. We can also use this annotation on private fields. Consider the below
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 5/23
14/04/2024, 19:29 Spring Boot Important Annotations
code:
package com.anjum.SpringBootDemo;
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private Person p;
When we use @Autowired on setter methods, Spring will try to perform the by Type
autowiring on the method. It means we are instructing Spring that it should initiate
this property using the setter method where you can add your custom code, such as
initializing any other property with this property. Consider the below code:
package com.anjum.SpringBootDemo;
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 6/23
14/04/2024, 19:29 Spring Boot Important Annotations
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
this.person=person;
@Configuration
The @Configuration annotation is used on classes which defines bean as the source
of the bean file. It is an analog for the XML configuration file. It is configured using
the Java class. A Java class that is annotated with this annotation is a configuration
itself and will import the methods to instantiate and configure the dependencies.
package com.anjum.SpringBootDemo;
import org.springframework.beans.factory.annotation.Autowired;
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 7/23
14/04/2024, 19:29 Spring Boot Important Annotations
import org.springframework.context.annotation.Configuration;
@Configuration
@Autowired
this.person=person;
@ComponentScan
The @ComponentScan annotation is used to scan a package for beans. It is used with
@Configuration annotation. We can also define the base package for scanning. If we
do not specify the base package, it will scan from the package of the class that
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 8/23
14/04/2024, 19:29 Spring Boot Important Annotations
package com.anjum.SpringBootDemo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan(basePackages = "com.anjum.SpringBootDemo")
@Configuration
// ...
@Bean
The @Bean annotation is an alternative of XML <bean> tag. If you ever have gone
through the <bean> tag, you must be familiar with it's working. It is a method level
annotation, which tells the method to produce a bean to be managed by the Spring
methods, which are annotated with @Bean annotation acts as bean ID and it creates
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 9/23
14/04/2024, 19:29 Spring Boot Important Annotations
@Bean
@Qualifier
more control over the dependency injection process. It can be specified on individual
while creating more bean files of the same type and we want to use only one of them
with a property.
and B2:
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 10/23
14/04/2024, 19:29 Spring Boot Important Annotations
@Component
//
@Component
//
If B1 autowires the BeanInterface, it is hard for Spring to decide which one of the
The solution to this problem is that we can use the @Qualifier annotation. Consider
@Component
public class B1 {
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 11/23
14/04/2024, 19:29 Spring Boot Important Annotations
@Autowired
@Qualifier("beanB2")
...
@Lazy
initializes all the autowired dependencies and configure the project at startup. This
annotation allows us to initialize the project components lazily, which means, the
When the @Lazy annotation is used on a @Configuration class; it will initialize all
package com.anjum
@Lazy
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 12/23
14/04/2024, 19:29 Spring Boot Important Annotations
@Configuration
@ComponentScan(basePackages = "com.anjum")
@Bean
@Bean
@Value
The @value annotation is used at filed, constructor parameter, and method parameter
level. It indicates a default value for the field or parameter to initialize the property
with. Its use is quite similar to @Autowired annotation; it also injects values from a
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 13/23
14/04/2024, 19:29 Spring Boot Important Annotations
property file into a bean's attribute. It supports both ${.....} and #{....} placeholders.
@Component
The @Component annotation is used at the class level to mark a Java class as a bean.
When we mark a class with @Component annotation, it will be found during the
classpath. The Spring framework will configure the annotated class in the application
package com.anjum;
@Component
.......
@Controller
commonly used to serve web pages. It returns a string having a redirect route.
@Service
The @Service annotation is also used at the class level to mark a service
package com.anjum;
@Service
//business code
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 15/23
14/04/2024, 19:29 Spring Boot Important Annotations
@Repository
The @Repository annotation is used on java classes which directly access the
database. It acts as a marker for any class that is used as a repository or DAO ( Data
Access Object).
This annotation has a built-in translation feature means when the class found an
exception, it will automatically handle with that; there is no need to add a try-catch
block manually.
@Repository
package com.anjum;
//
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 16/23
14/04/2024, 19:29 Spring Boot Important Annotations
The use of this annotation is degraded in Spring Boot 1.2.0 release because there is a
@SpringBootApplication
The @SpringBootApplication is the combination of three annotations
@EnableAutoConfiguration, @ComponentScan, and @Configuration.
setting up a new Spring Boot project. The class that is annotated with this annotation
must be placed in the base package. The main task of this annotation is to scan the
projects; it scans its sub-packages only. For example, if we annotate a class that is
The @Controller annotation can be used with Spring MVC and WebFlux. It is used at
the class level as a controller. It useful for detecting the component classes from the
classpath and autoregister the bean definition. The class annotated with this annotated
@RequestMapping
The @RequestMapping annotation is used to map the web requests. It can hold
several optional components such as consumes, header, method, name, params, path,
value, etc. This annotation can be used with classes as well as methods.
@Controller
@RequestMapping("/computer-science/books")
//application code
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 18/23
14/04/2024, 19:29 Spring Boot Important Annotations
return "itemList";
@GetMapping
The @GetMapping annotation is used to map the HTTP GET request on the specific
RequestMethod.GET).
@PostMapping
The @PostMapping annotation is used for mapping HTTP POST requests onto
RequestMethod.POST).
@PutMapping
The @PutMapping annotation is used to map the HTTP PUT requests on the specific
handler method. It is useful for creating a web service endpoint that creates or
RequestMethod.PUT).
@DeleteMapping
The @DeleteMapping is used to map the HTTP DELETE requests on the specific
handler method. It is useful for creating a web service endpoint that deletes a
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 19/23
14/04/2024, 19:29 Spring Boot Important Annotations
RequestMethod.DELETE).
@PatchMapping
The @PatchMapping is used to map the HTTP PATCH requests on the specific
RequestMethod.PATCH).
@RequestBody
annotation, the Spring framework wraps the incoming HTTP request body to that
parameter.
@ResponseBody
The @ResponseBody annotation is used to bind the method return value to the
@PathVariable
The @PathVariable annotation is used to extract the values from the URI. It is the
most suitable annotation for building the RESTful web service, where the URL
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 20/23
14/04/2024, 19:29 Spring Boot Important Annotations
@RequestParam
The @RequestParam annotation aka query parameter is used to extract the query
parameters from the URL. It is most suitable for developing web applications. It can
define the default values if the query parameter is not present in the URL.
@RequestHeader
The @RequestHeader annotation is used to get the details about the HTTP request
annotation for each detail in the header. It can be used repeatedly in a method.
@RestController
annotation. If the @RestController is used, then there is no need for annotating each
@RequestAttribute
attribute. It gives convenient access to the request attributes from a controller method.
Using this annotation, we can access objects that are populated on the server-side.
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 21/23
14/04/2024, 19:29 Spring Boot Important Annotations
@CookieValue
used as request mapping method’s argument. The HTTP cookie is bound for a given
cookie name to the @CookieValue parameter. It is used with the method that is
@CrossOrigin
The @CrossOrigin annotation is used both at the class & method level. It is used to
enable cross-origin requests. It useful for the cases, if a host serving JavaScript is
different from the data serving host. In such cases, the CORS ( Cross-Origin
The default behavior of this annotation allows all origins, all headers, the HTTP
However, we can control and customize the behavior by specifying the corresponding
attribute values.
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 22/23
14/04/2024, 19:29 Spring Boot Important Annotations
© 2024 About Accessibility User Agreement Privacy Policy Cookie Policy Copyright Policy Brand Policy Guest Controls Community Guidelines Language
https://www.linkedin.com/pulse/spring-boot-important-annotations-abid-anjum 23/23