Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Spring Boot Annotation?
3.
Spring Boot Annotations List
4.
Frequently Asked Questions
4.1.
What is Spring Boot Annotation?
4.2.
What is Spring Boot?
4.3.
Why are annotations used in Spring Boot?
5.
Key Takeaways
Last Updated: Jun 13, 2024

Spring Boot Annotations

Author Juhi Sinha
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Spring Boot is a Java-based open-source framework for developing microservices. Spring Boot Annotations give us a lot of flexibility in configuring Spring Framework's behavior.

In this article, we will learn the Spring Boot Annotations. Learning Spring Boot's annotations is not a big deal. So, without any further ado, let's get started!

Must Read, spring mvc vs spring boot

What is Spring Boot Annotation?

Spring Boot Annotations are metadata that provides information about a program that isn't contained within it. They have no direct impact on the code. They annotate functionality.

The convention over configuration principle is used instead of XML in Spring Boot Annotations. Because annotations provide complex functionality with almost no coding, they can significantly impact our development speed.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Spring Boot Annotations List

Let us read the different Spring Boot Annotations given below:

  1. @Bean
  2. @Service
  3. @Configuration
  4. @Repository
  5. @Controller
  6. @RequestMapping
  7. @Autowired
  8. @Component
  9. @EnableAutoConfiguration
  10. @SpringBootApplication
  11. @ComponetScan
  12. @Required
  13. @Qualifier
  14. @CookieValue 
  15. @Lazy
  • @Bean

The @Bean is used at the method levels, indicating that a method generates a bean that the Spring container should manage. It's a replacement for the XML <bean> tag.

Example:

@Bean
Public BeanExample beanExample ()
{
return new BeanExample (),
}

 

  • @Service

It represents that the annotated class is a service class, which can call external APIs and perform essential business logic.

Example:

@Service
public class TestService
{
public void service1()
{
// business code
}
}

 

  • @Configuration

        It's an excellent place to look for bean definitions. It's an annotation at the class level.

Example:

@Configuration
public class Bus
{
@BeanBus engine()
{
return new Bus();
}
}

 

  • @Repository

It's a Data Access Object (DAO) that connects directly to the database. It denotes the repository nature of the annotated class.

Example:

@Repository
public class TestRepository
{
public void delete()
{
// code
}
}

 

  • @Controller

The annotation signifies that the class is a web request handler. It is frequently used to display web pages. It's usually combined with the @RequestMapping annotation.

Example:

@Controller
@RequestMapping("Web")
public class WebNinjaController
{
@RequestMapping(value= "/{name}", method= RequestMethod.GET)
public Employee WebNinjaByName()
{
Return webNinjaTemplate;
}
}

 

  • @RequestMapping

The HTTP request is mapped using RequestMapping. It can be used with both the class and the method. There are numerous other optional elements, such as consumes, name, method, request, path, etc.

Example:

@Controller
public class NinjaController
{
@RequestMapping ("/web-course/ninjas")
public String getAllNinja(Model model)
{
//code of an application
return "ninjalist";
}

 

  • @Autowired

This annotation is used to auto-wire the spring bean on setter methods, constructors, and instance variables. It implicitly injects object dependency. When we use this annotation, the spring container automatically wires the bean to its matching data type.

Example:

@Component
public class Ninja
private Person person;
@Autowired
public Employee(Person person)
{
this.person=person
}
}

 

  • @Component

It's a class-level annotation that, during auto-scan, transforms the class into a Spring bean.

Example:

@Component
Public class Teachers
{
......
}

 

  • @EnableAutoConfiguration

It's in the application's main class. This annotation tells SpringBoot to start adding beans based on classpath settings, other beans, and various property settings. The use of this annotation was reduced in Spring Boot 1.2.0 because developers provided an alternative, namely @SpringBootApplication.

 

  • @SpringBootApplication

The @Configuration, @ComponentScan, and @EnabeAutoConfiguration are the three components of @SpringBootApplication. The @SpringBootApplication-annotated class is kept in the base package. This annotation performs the component scan. Only the sub-packages, however, are scanned.

  • @ComponetScan

A package of beans is scanned with it. It's used in conjunction with the annotation @Configuration to tell Spring which packages to look for annotated components. This annotation can also be used to indicate the base packages.

Example:

@ComponentScan(basePackages = "com.xyz")
@Configuration
Public class ScanComponent
{
//...
}

 

  • @Required

Bean setter methods are annotated with this annotation. It indicates that the required property in the affected bean must be filled at configuration time, or else an exception will be thrown: BeanInitializationException.

 

  • @Qualifier

It's used in conjunction with the @Autowired annotation and when more control over the dependency injection process is required. The @Qualifier is used to clear things up when more than one bean of the same type is created, but only one of them needs to be wired with a property.

 

  • @CookieValue 

It is used as an argument of the request mapping method at the method parameter level. The HTTP cookie is bound to a @CookieValue parameter for a given cookie name.

 

  • @Lazy

The component class uses it. All auto-wired dependencies are created and configured at startup. If a bean is to be initialized slowly, a @Lazy annotation can be used. This means that a bean will only be created if it is requested. It's also applicable to @Configuartion classes. It means that all @Bean methods in that @Configuration should be lazy initialized.

Must Read Spring Tool Suite

Frequently Asked Questions

What is Spring Boot Annotation?

Spring Boot Annotations are metadata that provides information about a program that isn't contained within it. They have no direct impact on the code they annotate functionality.
 

What is Spring Boot?

Spring Boot is a Java-based open-source framework for developing microservices. Pivotal Team created it, and it is used to create stand-alone and production-ready spring applications.
 

Why are annotations used in Spring Boot?

Spring Boot Annotations are a type of metadata that contains information about a particular program. Annotations, in other words, are used to provide additional information about a program. 

 

Key Takeaways

We’ve learned about the Spring Boot Annotations and their different types in this article. 

You can also check our previous blogs on STS Download, Introduction to Spring BootSpring Boot Auto-configuration, and Spring Boot CLI. If you are eager to learn advanced front-end web development, You can also consider our Spring Boot Course to give your career an edge over others.
 

Live masterclass