0% found this document useful (0 votes)
8 views4 pages

SpringBoot Annotations Guide

Uploaded by

aaditya902a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

SpringBoot Annotations Guide

Uploaded by

aaditya902a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Spring Boot Annotations - Definitions & Examples

1. Core Spring Boot & Configuration

@SpringBootApplication
Definition: Used to mark the main class of a Spring Boot application. It is a convenience annotation that adds
@Configuration, @EnableAutoConfiguration, and @ComponentScan. It helps Spring Boot auto-configure beans and
scan components.
Example:
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

@Configuration
Definition: Marks a class as a source of bean definitions. Used with @Bean methods to define beans for the Spring
container.
Example:
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig { }

@Bean
Definition: Used on methods to define beans to be managed by Spring. These beans can then be injected elsewhere in
the application.
Example:
@Bean
public MyService myService() {
return new MyService();
}

2. Web (Spring MVC - REST APIs)

@RestController
Definition: A combination of @Controller and @ResponseBody. It indicates that the class handles REST requests and
returns data directly as the response body.
Example:
@RestController
public class MyController { }

@RequestMapping
Definition: Maps web requests to handler methods. It can be used at the class or method level to define URL patterns
and HTTP methods.
Example:
@RequestMapping("/api")
public class ApiController { }
Spring Boot Annotations - Definitions & Examples

@GetMapping
Definition: Shortcut for @RequestMapping(method = RequestMethod.GET). It maps GET requests onto specific handler
methods.
Example:
@GetMapping("/hello")
public String sayHello() { return "Hello!"; }

@PostMapping
Definition: Shortcut for @RequestMapping(method = RequestMethod.POST). It maps POST requests onto specific
handler methods.
Example:
@PostMapping("/add")
public String addData(@RequestBody Data data) { return "Saved"; }

@PutMapping
Definition: Maps HTTP PUT requests to a specific handler method.
Example:
@PutMapping("/update")
public String updateData(@RequestBody Data data) { return "Updated"; }

@DeleteMapping
Definition: Maps HTTP DELETE requests to a specific handler method.
Example:
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable String id) { return "Deleted"; }

@RequestBody
Definition: Used to bind the HTTP request body to a method parameter. Commonly used for POST and PUT methods.
Example:
public String create(@RequestBody User user) { return user.getName(); }

@PathVariable
Definition: Binds a URI template variable to a method parameter.
Example:
public String getUser(@PathVariable String id) { return "User ID: " + id; }

3. Dependency Injection / Bean Management

@Component
Definition: Marks a Java class as a Spring component, allowing it to be automatically detected through classpath
scanning.
Example:
@Component
public class MyService { }

@Autowired
Definition: Automatically injects the dependency of a class field, constructor or method.
Spring Boot Annotations - Definitions & Examples

Example:
@Autowired
private MyService service;

@Slf4j
Definition: A Lombok annotation to automatically generate a logger instance named 'log'. Useful for logging within
classes.
Example:
@Slf4j
public class MyService {
public void serve() {
log.info("Serving...");
}
}

4. Persistence (Spring Data MongoDB & JPA)

@Document
Definition: Indicates that a class is a MongoDB document. The class will be mapped to a MongoDB collection.
Example:
@Document(collection = "users")
public class User { }

@Id
Definition: Marks the primary key field in a document or JPA entity.
Example:
@Id
private String id;

@Indexed
Definition: Indicates that a field should be indexed by the database to speed up searches.
Example:
@Indexed
private String email;

@DBRef
Definition: Used to define references between documents (similar to foreign keys).
Example:
@DBRef
private Address address;

@Transactional
Definition: Defines that the method should run within a transaction context. Ensures atomicity.
Example:
@Transactional
public void saveData() { ... }

@EnableTransactionManagement
Spring Boot Annotations - Definitions & Examples

Definition: Enables support for @Transactional annotation in the Spring application.


Example:
@EnableTransactionManagement
@SpringBootApplication
public class App { }

5. Security

@EnableWebSecurity
Definition: Enables Spring Security's web security configuration, allowing custom security rules to be applied.
Example:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { }

6. Lombok (Boilerplate Reduction)

@Data
Definition: A Lombok annotation that generates getters, setters, toString, equals, and hashCode methods automatically.
Example:
@Data
public class User {
private String name;
private int age;
}

@NonNull
Definition: Indicates that a method parameter or field cannot be null. Lombok generates null-checks.
Example:
public void setName(@NonNull String name) { this.name = name; }

@NoArgsConstructor
Definition: Generates a no-argument constructor automatically for the class.
Example:
@NoArgsConstructor
public class Product { }

You might also like