Spring Annotations - 230713 - 182106
Spring Annotations - 230713 - 182106
Spring Annotations - 230713 - 182106
Important Annotations
1. @Component Annotation:
@Component is a common annotation used to mark a Java class as a Spring-managed
component. It indicates that the class should be considered as a candidate for auto-detection
and automatic bean creation by Spring's component scanning mechanism.
Example:
@Component
public class MyComponent {
// Class implementation
}
2. @Autowired Annotation:
@Autowired is used to automatically wire dependencies by Spring's dependency injection
mechanism. It can be applied to fields, constructors, and setter methods, allowing Spring to
automatically resolve and inject the required dependencies.
Example:
@Component
public class MyComponent {
private MyDependency myDependency;
@Autowired
public MyComponent(MyDependency myDependency) {
this.myDependency = myDependency;
}
}
3. @Qualifier Annotation:
@Qualifier is used in conjunction with @Autowired to specify which bean should be autowired
when multiple beans of the same type are available. It helps to resolve ambiguity when
multiple beans match the required dependency.
Example:
@Component
public class MyComponent {
@Autowired
@Qualifier("myBean")
private MyInterface myBean;
}
4. @Primary Annotation:
@Primary is used to indicate a primary bean when multiple beans of the same type are
available. It specifies that a particular bean should be preferred when autowiring by type.
Example:
@Component
@Primary
public class MyPrimaryBean implements MyInterface {
// Class implementation
}
5. @SpringBootApplication:
@SpringBootApplication is a convenience annotation that combines @Configuration,
@EnableAutoConfiguration, and @ComponentScan. It is typically placed on the main
application class and enables the Spring Boot auto-configuration and component scanning
features.
Example:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Example:
@Configuration
public class MyConfiguration {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
Example:
@Controller
public class MyController {
// Class implementation
}
@Service
public class MyService {
// Class implementation
}
@Repository
public class MyRepository {
// Class implementation
}
8. @Lazy Annotation:
@Lazy is used to indicate that a bean should be lazily initialized, i.e., the bean will be created
only when it is first requested. This helps optimize the application's startup time.
Example:
@Component
@Lazy
public class MyLazyBean {
// Class implementation
}
9. @Scope Annotation:
@Scope is used to define the scope of a Spring bean. It allows you to specify whether a bean
should be singleton (default), prototype, request, session, or a custom scope.
Example:
@Component
@Scope("prototype")
public class MyPrototypeBean {
// Class implementation
}
Example:
@Component
public class MyComponent {
@Value("${my.property}")
private String myProperty;
}
11. @PropertySource and @PropertySources Annotations:
@PropertySource is used to specify the location of a properties file to be used for configuring
Spring beans. @PropertySources is used to specify multiple @PropertySource annotations.
Example:
@Configuration
@PropertySource("classpath:my.properties")
public class MyConfiguration {
// Class implementation
}
Example:
@Component
@ConfigurationProperties(prefix = "my")
public class MyConfiguration {
private String property1;
private String property2;
// Getters and setters
}
Example:
@Controller
public class MyController {
@ResponseBody
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Example:
@RestController
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Example:
@Controller
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Example:
@Controller
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Example:
@RestController
@RequestMapping("/api")
public class MyRestController {
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// Process the user data
return user;
}
}
Example:
@RestController
@RequestMapping("/api")
public class MyRestController {
@PutMapping("/users/{id}")
public User updateUser(@PathVariable("id") Long id, @RequestBody User user)
{
// Update the user with the specified ID
return user;
}
}
Example:
@RestController
@RequestMapping("/api")
public class MyRestController {
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable("id") Long id) {
// Delete the user with the specified ID
}
}
Example:
@RestController
@RequestMapping("/api")
public class MyRestController {
@GetMapping("/users/{id}")
public User getUserById(@PathVariable("id") Long id) {
// Retrieve and return the user with the specified ID
}
}
Example:
@RestController
@RequestMapping("/api")
public class MyRestController {
@GetMapping("/users")
public List<User> getUsersByRole(@RequestParam("role") String role) {
// Retrieve and return users based on the specified role
}
}
22. @EnableAutoConfiguration:
@EnableAutoConfiguration is used to enable Spring Boot's auto-configuration feature. It
automatically configures the Spring application based on the classpath and dependencies,
reducing the need for manual configuration.
Example:
@SpringBootApplication
@EnableAutoConfiguration
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
23. @ComponentScan:
@ComponentScan is used to enable component scanning in Spring. It specifies the base
package(s) to scan for Spring components such as @Component, @Controller, @Service, and
@Repository.
Example:
@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
These annotations play a crucial role in Spring applications, enabling various features such as
component scanning, dependency injection, request mapping, and configuration management.