English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot Annotations

Spring Boot annotations are a form of metadata that can provide information about the program. In other words, annotations are used to provide information about the Supplementaryinformation. It is not part of the application we are developing. It has no direct impact on the operation of the code it annotates. It will not change the operation of the compiled program.

In this section, we will discuss some important Spring Boot annotationsWe will use it in the later part of this tutorial.

Core Spring framework annotations

@Required: It applies to bean setting method. It indicates that necessary properties must be filled for the annotated Bean at configuration time, otherwise it will throw an exception BeanInitilizationException .

example

public class Machine 
{
private Integer cost;
@Required
public void setCost(Integer cost) 
{
    this.cost = cost;
}
public Integer getCost() 
{
    return cost;
}   
}

@Autowired: : Spring provides the @Autowired annotation to provide annotation-based automatic assembly. It is used to automatically connect setter methods, instance variables, and constructors on spring beans. When we use the @Autowired annotation, the spring container connects beans automatically by matching data types.

example

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

@Configuration: : It is a class-level annotation. A class annotated with @Configuration is used by Spring Containers as the source of bean definitions.

example

@Configuration
public class Vehicle
{
    @BeanVehicle engine()
    {
        return new Vehicle();
    }
}

@ComponentScan: : It is used when we want to scan beans in a package. It is used together with the annotation @Configuration. We can also specify the basic package used to scan Spring components.

example

@ComponentScan(basePackages = "com.w"3codebox"
@Configuration
public class ScanComponent
{
// ...
}

@Bean: is a method-level annotation. It is an XMLmarks a substitute method. It tells the method that produces a bean managed by the Spring Container.

example

@Bean
public BeanExample beanExample() 
{
    return new BeanExample();
}

Spring Framework structural annotation

@Component: . It is a class-level annotation. It is used to mark Java classes as Beans. When a class using @Component annotated Java class. The Spring framework picks it up and configures it in the application context as Spring Bean .

example

@Component
public class Student
{
    .......
}

@Controller: @Controller is a class-level annotation. It is @Component specialization. It marks a class as a web request handler. It is usually used for serving web pages. By default, it returns a string indicating the route to be redirected. It is usually used with @RequestMapping annotations are used together.

example

@Controller
@RequestMapping("books")
public class BooksController 
{
    @RequestMapping(value = ""/{name}, method = RequestMethod.GET)
    public Employee getBooksByName() 
    {
        return booksTemplate;
    }
}

@Service: is also used at the class level. It tells Spring that this class contains business logic.

example

package com.w3codebox;
@Service
public class TestService
{
    public void service1()
    {
        //business code
    }
}

@Repository: This is a class-level annotation. The repository is directly accessing the database DAO (Data Access Object). This repository performs all operations related to the database.

package com.w3codebox;
@Repository 
public class TestRepository
{
    public void delete()
    {   
        //persistence code
    }
}

Spring Boot annotations

@EnableAutoConfiguration: : It automatically configures the bean existing in the classpath and configures it to run the method. In Spring Boot 1.2.0 release reduced the use of this annotation because developers provided an alternative method for this annotation, namely @SpringBootApplication . @SpringBootApplication: : It is three annotations @EnableAutoConfiguration, @ComponentScan,and @Configuration combination.

Spring MVC and REST annotations

@RequestMapping: to mapnetwork requests. It has many optional elements, such as consumes, header, method, name, params, path, produces, and value. We use it together with classes and methods.

example

@Controller
public class BooksController 
{
    @RequestMapping("/computer-science/books)
    public String getAllBooks(Model model)
    {
        //application code
        return "bookList";
    }
}

@GetMapping: It maps HTTP GET maps the request to a specific handler method. It is used to createretrievethe Web service endpoint, instead of using @RequestMapping(method = RequestMethod.GET) @PostMapping It maps HTTP POST maps the request to a specific handler method. It is used to createcreatethe Web service endpoint, instead of using: @RequestMapping(method = RequestMethod.POST) @PutMapping: It maps HTTP PUT maps the request to a specific handler method. It is used to createcreateorupdatethe Web service endpoint, instead of using: @RequestMapping(method = RequestMethod.PUT) @DeleteMapping: It maps HTTP DELETE maps the request to a specific handler method. It is used to createdeletethe Web service endpoint of the resource. Use it instead of using: @RequestMapping(method = RequestMethod.DELETE) @PatchMapping: It maps HTTP PATCH maps the request to a specific handler method. Use it instead of using: @RequestMapping(method = RequestMethod.PATCH) @RequestBody: to bind the HTTP request body to the object in the method parametersbinding. Internally, it uses HTTP MessageConverters Convert the request body. When we use When the @RequestBody annotation is used on method parameters, the Spring framework binds the incoming HTTP request body to the parameter. @ResponseBody: It binds the method return value to the response body. It tells the Spring Boot Framework to serialize the returned object into JSON and XML formats. @PathVariable: Used to extract values from the URI. It is most suitable for RESTful Web services where the URL contains path variables. We can define multiple @PathVariable in one method. @RequestParam: Used to extract query parameters from the URL. Also known asQuery parameter.It is most suitable for web applications. If there are no query parameters in the URL, you can specify a default value. @RequestHeader: Used to get detailed information about HTTP request headers. We use this annotation asMethod parameter.The optional elements of the annotation areName, required, value, defaultValue. For each detail in the title, we should specify a separate annotation. We can use it multiple times in one method @RestController: It can be considered as @Controller and @ResponseBody Annotationcombination. The @RestController annotation itself is annotated with @ResponseBody annotation. There is no need to use @ResponseBody annotation for each method. @RequestAttribute: It binds method parameters to request attributes. It provides methods for conveniently accessing request attributes from controller methods. With the @RequestAttribute annotation, we can access objects filled on the server side.

Note: We have used all the above examples and real examples in the RESTful Web Service tutorial.