0% found this document useful (0 votes)
15 views7 pages

Annottions Springboot

The document provides a comprehensive overview of important Spring Boot annotations categorized into core, REST and web, dependency injection, database and JPA, configuration and profile management, and security annotations. Each annotation is described with its purpose and includes examples for clarity. These annotations facilitate automated configurations, bean management, and core functionalities, streamlining the development process in Spring Boot applications.

Uploaded by

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

Annottions Springboot

The document provides a comprehensive overview of important Spring Boot annotations categorized into core, REST and web, dependency injection, database and JPA, configuration and profile management, and security annotations. Each annotation is described with its purpose and includes examples for clarity. These annotations facilitate automated configurations, bean management, and core functionalities, streamlining the development process in Spring Boot applications.

Uploaded by

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

Spring Boot provides a wide range of annotations to simplify development

by automating configurations, managing beans, and implementing core


functionalities. Below is a categorized list of important Spring Boot
annotations and their purposes:

1. Core Spring Boot Annotations

 @SpringBootApplication

o Combines @Configuration, @EnableAutoConfiguration, and


@ComponentScan.

o Marks the main class of a Spring Boot application.

o Example:

o @SpringBootApplication

o public class MyApplication {

o public static void main(String[] args) {

o SpringApplication.run(MyApplication.class, args);

o }

o }

 @Configuration

o Indicates that a class contains Spring bean definitions.

o Example:

o @Configuration

o public class AppConfig {

o @Bean

o public MyService myService() {

o return new MyService();

o }

o }

 @EnableAutoConfiguration

o Enables Spring Boot's auto-configuration mechanism.

o Included in @SpringBootApplication.
 @ComponentScan

o Scans packages for components, configurations, and services.

o Example:

o @ComponentScan(basePackages = "com.example.service")

2. REST and Web Annotations

 @RestController

o Combines @Controller and @ResponseBody.

o Used for creating RESTful web services.

o Example:

o @RestController

o public class MyController {

o @GetMapping("/hello")

o public String sayHello() {

o return "Hello, World!";

o }

o }

 @Controller

o Marks a class as a web controller.

o Typically used for returning views (e.g., Thymeleaf templates).

 @RequestMapping

o Maps HTTP requests to handler methods or classes.

o Example:

o @RequestMapping("/api")

o public class ApiController {

o @GetMapping("/users")

o public List<User> getUsers() {

o return userService.findAll();

o }
o }

 @GetMapping, @PostMapping, @PutMapping,


@DeleteMapping

o Shortcut annotations for HTTP methods.

o Example:

o @PostMapping("/add")

o public ResponseEntity<String> addUser(@RequestBody User


user) {

o userService.save(user);

o return ResponseEntity.ok("User added");

o }

 @PathVariable

o Binds a method parameter to a URI template variable.

o Example:

o @GetMapping("/users/{id}")

o public User getUser(@PathVariable("id") Long id) {

o return userService.findById(id);

o }

 @RequestParam

o Extracts query parameters from the request URL.

o Example:

o @GetMapping("/search")

o public List<User> search(@RequestParam String name) {

o return userService.searchByName(name);

o }

 @RequestBody

o Maps the request body to a method parameter.

o Example:

o @PostMapping("/users")
o public void addUser(@RequestBody User user) {

o userService.save(user);

o }

 @ResponseBody

o Indicates that a method return value should be written directly


to the HTTP response.

3. Dependency Injection Annotations

 @Component

o Marks a class as a Spring-managed component.

o Example:

o @Component

o public class MyComponent { }

 @Service

o A specialization of @Component used for business logic.

 @Repository

o A specialization of @Component used for data access logic.

 @Autowired

o Injects a bean automatically by type.

o Example:

o @Service

o public class MyService {

o @Autowired

o private MyRepository repository;

o }

 @Qualifier

o Used with @Autowired to resolve ambiguity when multiple


beans of the same type exist.

o Example:
o @Autowired

o @Qualifier("specificBean")

o private MyBean myBean;

 @Value

o Injects values from properties files.

o Example:

o @Value("${app.name}")

o private String appName;

4. Database and JPA Annotations

 @Entity

o Marks a class as a JPA entity.

o Example:

o @Entity

o public class User { }

 @Id

o Specifies the primary key for an entity.

 @GeneratedValue

o Specifies how the primary key should be generated.

o Example:

o @Id

o @GeneratedValue(strategy = GenerationType.IDENTITY)

o private Long id;

 @Table

o Specifies the database table for an entity.

o Example:

o @Table(name = "users")

 @Repository

o Indicates that the class is a Spring Data repository.


o Example:

o @Repository

o public interface UserRepository extends JpaRepository<User,


Long> { }

5. Configuration and Profile Management

 @Bean

o Indicates a method that produces a Spring-managed bean.

o Example:

o @Bean

o public RestTemplate restTemplate() {

o return new RestTemplate();

o }

 @Profile

o Specifies the profile under which a component or bean should


be active.

o Example:

o @Profile("dev")

o @Bean

o public DataSource devDataSource() { }

 @PropertySource

o Specifies a properties file to load.

o Example:

o @PropertySource("classpath:application.properties")

6. Security Annotations

 @PreAuthorize

o Specifies method-level security using SpEL (Spring Expression


Language).

o Example:
o @PreAuthorize("hasRole('ADMIN')")

o public void secureMethod() { }

 @EnableWebSecurity

o Enables Spring Security's web security support.

 @Secured

o Restricts access to methods based on roles.

o Example:

o @Secured("ROLE_ADMIN")

These annotations are the building blocks of Spring Boot, making


development faster and more intuitive. Let me know if you'd like to dive
deeper into any specific annotation or concept!

You might also like