1/20
Regarding Spring Boot Annotations for Semster
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Anotations
Annotations are a form of meta data that provides data about a program
Provides Supplemental information about a program
Does not have a direct effect on the operation of the code
Types of Spring Boot Annotation
Spring Core Annotation
Spring Web Annotation
Spring Web Annotation
Spring Scheduling Annotation
Spring Data Annotation
Spring Bean Annotation
Core Annotation
These Annotations belong to the spring core module
They provide meta data to manage beans, dependency injection and control the application context without requiring a XML file
Core Annotation Example
Dependency Injection Relate Annotations
@Autowired
@Qualifier
@Bean
@Primary
@Lazy
Context Configuration Annotation
@Profile
@Import
@Import Resource
Dependency Injection related Annotations Code Example
//Field Injection
class Student{
@Autowired
Address address;}
//Constructor Injection
class Student{
@Autowired Student(){
//Setter Injection
class Student{
Address address;
@Autowired
void setAddress(Address ad){
address = ad;}
Context Configuration Annotations Code Example
@Component @Profile("developer") class Student{}
Spring Web Annotation
Spring Web Annotations are a part of the Spring MVC Framework
Used to build web applications and restful API
help to map HTTP request to Java methods, data binding, and define controller behaviour
Examples for Spring Web Annotation
@Request Mapping
@Response Body
@Path Variable
@Controller
Request Mapping Code
@Request mapping (value = "url")
class Demo{
@Get Request("url continuation")
some function to be executed
@Request Mapping is a class level annotation
Used to map HTTP request to their respective methods
It is a generic annotation as it can be used for any HTTP method like GET, POST, PUT, DELETE.
@Response Body
@Response Body
//Followed by similar code to before
It can be applied on both classes and methods
It tells the spring to return the method’s return value as the response body(ex : JSON)
@Controller
@Controller
//Followed by request mapping code
Marks a Java class as a web controller making it responsible for handling HTTP requests
Spring Boot Annotations
These simplify the development of restful API
Work on top of the spring framework and focus on auto configuration
Examples for Spring Boot Annotation
@Spring Boot Application
@Enable Auto Configuration
Auto-Configuration Conditions
@Conditional on Class
@Conditional on Bean
@Conditional on Property
@Conditional on Resource
@Conditional on Web Application
@Conditional on Not Web application
@Conditional
Example for @Spring Boot Application
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
This annotation is used to mark the main class of a spring boot application
It encapsulates @Configuration, @Enable Auto Configuration, @Component Scan
Spring Scheduling Annotation
Used to run tasks at a fixed interval or according to a cron expression
Helpful for automated tasks
Example for Scheduling Annotations
@Enable Async
@Enable Scheduling
@Scheduled
@Async
Spring Data Annotations
Used to map Java classes with database entities
Simplify data access when using JPA, Mongo DB or other spring modules
Spring Data Annotations Example
Common data Annotations
@Id
@Params
@Created By
@Last Modified By
@Created Date
Common Spring JPA Annotations
@Query
@Procedure
@Lock
Common Mongo Annotation
@Query
@Document
@Field
Spring Bean Annotation
Used to define and manage beans in a spring container
Essential for making objects available for dependency Injection
Examples for Spring Bean Annotation
@Component
@Component Scan
@Configuration