Spring Annotations
Spring Annotations provide a convenient and declarative way to configure and manage Spring beans and their dependencies. This guide covers the key annotations used in Spring for various purposes, such as component scanning, dependency injection, configuration, and more.
Core Spring Annotations
- @Component: Indicates that a class is a Spring component.
- @Service: Indicates that a class is a Spring service.
- @Repository: Indicates that a class is a Spring repository (DAO).
- @Controller: Indicates that a class is a Spring controller.
// Example of core Spring annotations
@Component
public class MyComponent {
// Component logic
}
@Service
public class MyService {
// Service logic
}
@Repository
public class MyRepository {
// Repository logic
}
@Controller
public class MyController {
// Controller logic
}
Dependency Injection Annotations
- @Autowired: Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities.
- @Qualifier: Specifies the qualifier to be used when autowiring beans of the same type.
- @Inject: A JSR-330 annotation that is functionally equivalent to @Autowired.
- @Value: Injects values from properties files or environment variables.
// Example of dependency injection annotations
@Component
public class MyBean {
@Autowired
private MyDependency myDependency;
@Autowired
public MyBean(MyDependency myDependency) {
this.myDependency = myDependency;
}
@Autowired
public void setMyDependency(MyDependency myDependency) {
this.myDependency = myDependency;
}
@Value("${my.property}")
private String myProperty;
// Bean logic
}
@Component
public class MyDependency {
// Dependency logic
}
Configuration Annotations
- @Configuration: Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests.
- @Bean: Indicates that a method produces a bean to be managed by the Spring container.
- @PropertySource: Declares a set of properties files to be loaded.
// Example of configuration annotations
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean(myDependency());
}
@Bean
public MyDependency myDependency() {
return new MyDependency();
}
}
Spring MVC Annotations
- @RequestMapping: Maps HTTP requests to handler methods of MVC and REST controllers.
- @GetMapping: Shortcut for @RequestMapping(method = RequestMethod.GET).
- @PostMapping: Shortcut for @RequestMapping(method = RequestMethod.POST).
- @PutMapping: Shortcut for @RequestMapping(method = RequestMethod.PUT).
- @DeleteMapping: Shortcut for @RequestMapping(method = RequestMethod.DELETE).
- @PathVariable: Indicates that a method parameter should be bound to a URI template variable.
- @RequestParam: Indicates that a method parameter should be bound to a web request parameter.
- @RequestBody: Indicates that a method parameter should be bound to the body of the web request.
- @ResponseBody: Indicates that a method return value should be bound to the web response body.
// Example of Spring MVC annotations
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring!";
}
@GetMapping("/greet/{name}")
public String greet(@PathVariable String name) {
return "Hello, " + name + "!";
}
@PostMapping("/data")
public void postData(@RequestBody MyData data) {
// Handle POST request
}
}
public class MyData {
private String value;
// Getters and setters
}
Transaction Management Annotations
- @Transactional: Declares that a method or class must be executed within a transactional context.
// Example of @Transactional annotation
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
@Transactional
public void performTransaction() {
// Transactional logic
myRepository.save(new MyEntity());
}
}
Asynchronous Execution Annotations
- @Async: Indicates that a method should be executed asynchronously.
- @EnableAsync: Enables Spring's asynchronous method execution capability.
// Example of @Async and @EnableAsync annotations
@Configuration
@EnableAsync
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@Service
public class MyService {
@Async
public void performAsyncTask() {
// Asynchronous logic
}
}
Key Points
- Spring Annotations provide a declarative way to configure and manage Spring beans and their dependencies.
- Core annotations include @Component, @Service, @Repository, and @Controller.
- Dependency Injection annotations include @Autowired, @Qualifier, @Inject, and @Value.
- Configuration annotations include @Configuration, @Bean, and @PropertySource.
- Spring MVC annotations include @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PathVariable, @RequestParam, @RequestBody, and @ResponseBody.
- Transaction management annotation @Transactional manages transactions declaratively.
- Asynchronous execution annotations @Async and @EnableAsync enable asynchronous method execution.
Conclusion
Spring Annotations provide a powerful and convenient way to configure and manage beans, dependencies, and various aspects of application behavior. By leveraging these annotations, developers can create clean, maintainable, and efficient Spring applications. Happy coding!