Spring Boot Key Annotations

Spring Boot is widely used for building basic to enterprise level applications. Hence it is important to understand some of the important annotations, which, every spring developer must know. Below is the list of the key Spring Boot annotations –

  1. @Bean
  2. @Autowired
  3. @Configuration
  4. @Component
  5. @ComponentScan
  6. @Repository
  7. @Service
  8. @Controller
  9. @RestController
  10. @EnableAutoConfiguration
  11. @SpringBootApplication
  12. @RequestMapping
  13. @GetMapping
  14. @PostMapping
  15. @PutMapping
  16. @DeleteMapping

Each of the above mentioned annotation is explained below in detail –

@Bean – Bean is a method level annotation. It directs method to create a Spring container managed bean.

Example –

@Bean
public TestBean testBeanMethod()
{
      return new TestBean();
}

@Autowired – Autowired annotation can be used at various places for the purpose of auto wiring Spring bean at method level, at setter method, at constructor or for config method level.

Example –

@Component
public class TestAutoWiring
{
      private TestAutoWiring test;
      
      @Autowired
      public TestAutoWiring(TestAutoWiring test)
      {
            this.test = test;
      }
}

@Configuration – Configuration is a class level annotation. The class which has one or more @Bean annotations shall be annotated with @Configuration annotation.

Example –

@Configuration
public class ConfigClass
{
      @Bean
      public TestBean testBeanMethod()
      {
          return new TestBean();
      }
}

@Component – Component is a class level annotation. This indicates that the class where this annotation is used is a Component. These classes are found by Spring and configure in application context as Spring Bean. Component class is identified during classpath classes identification.

Example –

@Component
public class ComponentClass
{
       public String methodOne();
       public String methodTwo();
}

@ComponentScan – Component is a class level annotation. It is used with @Configuration annotation for scanning specified packages.

Example –

@ComponentScan (basePackages="com.thinkconstructive")
@Configuration
public class ComponentClass
{
       public String methodOne();
       public String methodTwo();
}

@Repository – Repository is a class level annotation. This indicates that the class where this annotation is used is a Repository and is specialised Component. Class annotated with this annotation performs all the database related operations. This is a DAO – Data Access Object , which accesses database directly. Repository class will be auto detected.

Example –

@Repository
public class RepositoryClass
{
       public TestObj findById()
       {
           // Fetches object from database
           return testObj;
       }
}

@Service – Service is a class level annotation. This indicates that the class where this annotation is used is a Service class and is specialised Component. Class annotated with this annotation performs act as a Business Service Facade i.e. business logic is generally written in this class. Service class will be auto detected.

Example –

@Service
public class ServiceClass
{
       public TestObj createUser()
       {
           // Performs basic validation before sending to database
           return testObj;
       }
}

@Controller – Controller is a class level annotation. This indicates that the class where this annotation is used is a Controller class and does the job of Controller that means the respective class is marked as web request handler class.

Example –

@Controller
//Request mapping
public class ControllerClass
{
       // Get request mapping
       public String getUser()
       {
           // Receives web request and returns response
           return "testObj""Success;
       }
}

@RestController – RestController is a class level annotation. It is a specialised version of Controller annotation. This indicates that the class where this annotation is used is a REST Controller class and does the job of handling REST request and responses.

Example –

@RestController
//Request mapping
public class ControllerClass
{
       // Get request mapping
       public String getUser()
       {
           // Receives Get request and returns response
           return "testObj""Success;
       }
}

@SpringBootApplication – SpringBootApplication is a class level annotation. It is the combination of @EnableAutoConfiguration, @ComponentScan and @Configuration. Any Spring Boot application main class / entry point class is always annotated with this annotation.

@SpringBootApplication = @EnableAutoConfiguration + @ComponentScan + @Configuration

Example –

@SpringBootApplication
public class SpringBootMainClass
{
	public static void main(String[] args) {
		SpringApplication.run(SpringBootMainClass.class, args);
	}
}

@EnableAutoConfiguration – EnableAutoConfiguration is a class level annotation. It auto configures the classes available in the configured classpath and the beans defined in the application. This annotation is bundled with @SpringBootApplication annotation and hence is implicitly used with @SpringBootAnnotation.

Example –

@SpringBootApplication
public class SpringBootMainClass
{
	public static void main(String[] args) {
		SpringApplication.run(SpringBootMainClass.class, args);
	}
}

@RequestMapping – RequestMapping annotation can be used at class level as well as method level. It is used to set the context path for the web request.

Example –

@RestController
@RequestMapping ("/user")
public class ControllerClass
{
       @RequestMapping(method=RequestMethod.GET,path="/{userId}")
       public String getUser()
       {
           // Receives Get request and returns response
           return userObj;
       }
}

@GetMapping – GetMapping is a method level annotation. It is used to expose the Get URL for the web requests.

Example –

@RestController
@RequestMapping ("/user")
public class ControllerClass
{
       @GetMapping("/{userId}")
       public String getUser()
       {
           // Receives Get request and returns response
           return userObj;
       }
}

@PostMapping – PostMapping is a method level annotation. It is used to expose the Post URL for the web requests.

Example –

@RestController
@RequestMapping ("/user")
public class ControllerClass
{
       @PostMapping("/")
       public String createUser(User user)
       {
           // Receives Post request, saves data and returns response
           return "Success";
       }
}

@PutMapping – PutMapping is a method level annotation. It is used to expose the Put/ Update URL for the web requests.

Example –

@RestController
@RequestMapping ("/user")
public class ControllerClass
{
       @PutMapping("/")
       public String updateUser(User user)
       {
           // Receives Put request, updates data and returns response
           return "Success";
       }
}

@DeleteMapping – DeleteMapping is a method level annotation. It is used to expose the Delete URL for the web requests.

Example –

@RestController
@RequestMapping ("/user")
public class ControllerClass
{
       @DeleteMapping("/")
       public String deleteUser(User user)
       {
           // Receives Delete request, deletes data and returns response
           return "Success";
       }
}
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *