Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Spring Boot Annotations

Spring boot

Uploaded by

siddharth90750
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)
7 views4 pages

Spring Boot Annotations

Spring boot

Uploaded by

siddharth90750
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 (with Explanations)

1. Core Annotations
@SpringBootApplication
• Purpose: Marks the main class of a Spring Boot application.
• Explanation: Combines @Configuration, @EnableAutoConfiguration, and
@ComponentScan.
• Usage:
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}

@Configuration
• Purpose: Indicates that the class can be used by Spring IoC container as a source of bean
definitions.
• Usage:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}

@ComponentScan
• Purpose: Automatically discovers and registers beans in specified packages.
• Usage:
@ComponentScan(basePackages = "com.example.myapp")
public class AppConfig {}

@EnableAutoConfiguration
• Purpose: Enables Spring Boot’s auto-configuration mechanism.
• Usage:
@EnableAutoConfiguration
public class AppConfig {}
2. Bean Annotations
@Component
• Purpose: Marks a Java class as a Spring-managed component.
• Usage:
@Component
public class MyComponent {}

@Service
• Purpose: Specialized @Component annotation, indicating a service layer.
• Usage:
@Service
public class MyService {}

@Repository
• Purpose: Specialized @Component annotation for the persistence layer.
• Usage:
@Repository
public class MyRepository {}

@Bean
• Purpose: Declares a Spring bean in @Configuration classes.
• Usage:
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}

3. Web Layer Annotations


@RestController
• Purpose: Combination of @Controller and @ResponseBody.
• Usage:
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}

@RequestMapping
• Purpose: Maps HTTP requests to handler methods.
• Usage:
@RequestMapping("/api")

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping


• Purpose: Specialized annotations for HTTP methods.
• Usage:
@GetMapping("/users")
public List<User> getUsers() {
return userService.getAllUsers();
}

4. Dependency Injection Annotations


@Autowired
• Purpose: Automatically injects dependencies.
• Usage:
@Autowired
private MyService myService;

@Qualifier
• Purpose: Specifies which bean to inject when multiple candidates exist.
• Usage:
@Autowired
@Qualifier("specificBeanName")
private MyService myService;

@Value
• Purpose: Injects values from properties files.
• Usage:
@Value("${app.name}")
private String appName;

5. Conditional Annotations
@ConditionalOnProperty
• Purpose: Configures beans based on environment properties.
• Usage:
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public MyFeatureBean myFeatureBean() {
return new MyFeatureBean();
}
@ConditionalOnMissingBean
• Purpose: Defines a bean only if another specific bean is missing.
• Usage:
@Bean
@ConditionalOnMissingBean
public MyDefaultBean myDefaultBean() {
return new MyDefaultBean();
}

6. Transaction and Validation Annotations


@Transactional
• Purpose: Marks a method or class as transactional.
• Usage:
@Transactional
public void saveUser(User user) {
userRepository.save(user);
}

@Valid & @Validated


• Purpose: Validates request bodies or method arguments.
• Usage:
@PostMapping("/users")
public ResponseEntity<?> createUser(@Valid @RequestBody User user) {
return ResponseEntity.ok(userService.save(user));
}

7. Spring Boot Testing Annotations


@SpringBootTest
• Purpose: Provides a Spring context for integration testing.
• Usage:
@SpringBootTest
public class MyApplicationTests {
@Test
public void contextLoads() {
}
}

@MockBean & @SpyBean


• Purpose: Create mock or spy beans for unit testing.
• Usage:

You might also like