Spring Boot Annotations

We can define it as of metadata that gives basic information about a program. That is, annotations are utilized to give further information about the program. It is not the portion of the application that is being developed. It has no direct effect on the behaviour of the annotation code. The behaviour of the compiled program does not change.

The following part describes some major Spring Boot annotations used further in this tutorial.

Core Spring Framework Annotations

@ Required: When we discuss @Required annotation, majorly, it is utilized to enter into bean setter methods. It indicates that you must enter the required properties for the annotated bean during configuration. Otherwise, a BeanInitilizationException will be thrown. The following example depicts the same:

Example:

public class Fuel 
{  
private Integer costing;  
@Required  
public void setCosting(Integer costing)   
{  
this.costing = costing;  
}  
public void getCosting()   {  
}     
 }  

@Autowired: This is another annotation offered by Spring that is annotation-based automatic wiring by giving @Autowired annotations. Automatically connect Spring Beans to setter methods, instance variables and constructors. Using the @Autowired annotation, the Spring container will automatically connect the bean by collating the data type.

Example:

@Component
public class Apparel
{
private shirt shirt;
@Autowired
public Clothes(Shirt shirt)
{
this.shirt=shirt;
}
}

@Configuration: This is a class-level annotation. @Configuration annotated class used by SpringContainers as the source for Bean definitions

Example:

@Configuration  
public class Car
{  
@BeanVehicle wheel()  
{  
return new Car();  
}  
}  

@ComponentScan: Used to scan the bean of a packet. Used with the @Configuration annotation. You can also specify a base package to scan Spring components.

Example:

@ComponentScan(basePackages = "url")  
@Configuration  
public class Abc 
{  
//content..
}  

@Bean: We can define @Bean annotation as a method-level annotation. It is a substitute to the XML <bean> tag. It directs the method that will generate the beans managed by Spring Container.

Example:

@Bean  
public BeanEg beanEg()   
{  
return new BeanEg ();  
}  

Spring Framework Stereotype Annotations

@Component: We can define @Component as an annotation equivalent to a class. The utilization of this is to symbolize a Java class as a bean. We can find Java classes with @Component annotation in the Java class path. The Spring Framework gets and configures it as a bean in the application environment.

Example:

@Component  
class Customer 
{  
.......  
}  

@Controller: We can also define @Controller as an annotation equivalent to class. We can say that it is a speciality of @Component only. The major difference is that it marks the class as a web request handler. Utilization of this is to serve web pages. By default, a string indicating the route to redirect to is returned. It is mainly used in the @RequestMapping annotation.

Example:

@Controller  
@RequestMapping("library")  
public class Example  
{  
@RequestMapping(value = "/{title}")  
public Employee getBooksByTitle ()   
{  
return booksTitle;  
}  
}  

@Service: Also used at the class level. Notifies Spring about the business arguments that depend on class.

Example:

package url;  
@Service  
public class abc  
{  
public void s1()  
{  
//business code  
}  
}  

@Repository: We can define @Repository as a class-level annotation. A repository is a DAOs (Data Access Object) directly accessing a database. It brings out every function that is associated with the database.

package com.tutorialandexample;  
@Repository   
class Example 
{  
public void delete()  
{     
//content  
}  
}  

Spring Boot Annotations

  • @EnableAutoConfiguration: Automatically beans in class-path and configures to execute the method. This annotation has been reduced in the Spring Boot 1.2.0 release. Due to that, the developers have provided an alternative to annotations defined as @SpringBootApplication.
  • @SpringBootApplication: A blend of the following annotations: @EnableAutoConfiguration, @ComponentScan, and @configuration.

Spring MVC and REST Annotations

  • @RequestMapping: We can describe @RequestMapping as an annotation that is utilized to map web requests. It contains many optional elements such as "consumes", "header", "method", "name", "params", "path", and “produces", "value". Used in both classes and methods.

Example:

@Controller  
public class Example 
{  
@RequestMapping("/Marvels/movies")  
public String movies(Model model)  
{  
//code  
return "allMovies";  
}  
  • @GetMapping: This annotation locates the HTTP GET requests to a particular handler method. Utilization of this annotation is to build a web service endpoint rather than to poll instead: @RequestMapping (method = RequestMethod)
  • @PostMapping: This annotation locates the HTTP POST requests to a particular handler method. This annotation is also utilized to build a web service endpoint that creates. Rather than using the following annotation: @RequestMapping(method = RequestMethod.POST), we use this annotation.