Spring Core and Boot Annotations - tarunchhabra/parakalo GitHub Wiki

https://www.baeldung.com/spring-core-annotations#required

@Bean @Autowired @Required @Qualifier @Value- We can use @Value for injecting property values into beans. It's compatible with constructor, setter, and field injection. https://www.baeldung.com/spring-value-annotation

@Lazy- https://www.baeldung.com/spring-lazy-annotation @Scope @ComponentScan

@DependsOn- Spring guarantees that the defined beans will be initialized before attempting an initialization of the current bean.

We can choose either the SmartLifeCycle interface or the @DependsOn annotation for managing initialization order.

Let's say we have a FileProcessor which depends on a FileReader and FileWriter. In this case, FileReader and FileWriter should be initialized before the FileProcessor.

@DependsOn annotation behavior in case of a missing bean or circular dependency. Or in case of simply needing one bean initialized before another.

Note: While using @DependsOn, we must use component-scanning If a DependsOn-annotated class is declared via XML, DependsOn annotation metadata is ignored

https://www.baeldung.com/spring-depends-on

Spring Bean Annotations

common Spring bean annotations used to define different types of beans.

There're several ways to configure beans in a Spring container. We can declare them using XML configuration. We can declare beans using the @Bean annotation in a configuration class. We can mark the class with one of the annotations from the org.springframework.stereotype package and leave the rest to component scanning.

@Component @Configuration @Service @Controller

Here's a quick overview of a few of these annotations:

@Component is a generic stereotype for any Spring-managed component @Service annotates classes at the service layer @Repository annotates classes at the persistence layer, which will act as a database repository

https://www.baeldung.com/spring-component-repository-service https://www.baeldung.com/spring-bean-annotations

@Controller and @RestController Annotations

The @RestController annotation was introduced in Spring 4.0 to simplify the creation of RESTful web services. It's a convenience annotation that combines @Controller and @ResponseBody – which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.

https://www.baeldung.com/spring-controller-vs-restcontroller

Spring Web Annotations

@RequestMapping- Simply put, @RequestMapping marks request handler methods inside @Controller classes.

https://www.baeldung.com/spring-new-requestmapping-shortcuts

spring-mvc-annotations

https://www.baeldung.com/spring-mvc-annotations

@PathVariable

@RequestMapping("/{id}") Vehicle getVehicle(@PathVariable("id") long id) { // ... }

@RequestParam- for accessing HTTP request query parameters.

@RequestBody

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object.

Response Handling Annotations

@ResponseBody

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

https://www.baeldung.com/spring-request-response-body

@ResponseStatus- We can specify the desired HTTP status of the response if we annotate a request handler method with this annotation. We can declare the status code with the code argument, or its alias, the value argument.

@ExceptionHandler- With this annotation, we can declare a custom error handler method. Spring calls this method when a request handler method throws any of the specified exceptions.

@ExceptionHandler(IllegalArgumentException.class) void onIllegalArgumentException(IllegalArgumentException exception) { // ... }

Spring Boot Annotations

@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

https://www.baeldung.com/spring-boot-annotations

@CrossOrigin @CrossOrigin enables cross-domain communication for the annotated request handler methods

https://www.baeldung.com/spring-cors

https://www.baeldung.com/spring-data-annotations

https://www.baeldung.com/spring-enable-annotations

https://www.baeldung.com/springbootconfiguration-annotation https://www.baeldung.com/configuration-properties-in-spring-boot

https://www.baeldung.com/spring-async