Dependency injection is a key concept in Spring Boot that enables the management and injection of dependencies into your application components. Spring Boot provides several annotations to facilitate dependency injection and reduce boilerplate code. In this section, we will explore the commonly used dependency injection annotations and demonstrate how they can be effectively used in your Spring Boot applications.
1. @Autowired:
The `@Autowired` annotation is one of the most commonly used annotations for dependency injection in Spring Boot. It allows the Spring container to automatically wire dependencies into a class. Consider the following example:
“`java
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
// …
}
“`
In this example, the `MyService` class has a dependency on the `MyRepository` interface. By annotating the `myRepository` field with `@Autowired`, Spring will automatically inject an instance of `MyRepository` into the `myRepository` field when the `MyService` bean is created.
2. @Qualifier:
Sometimes, there may be multiple beans of the same type in the Spring container, and you need to specify which one to inject. The `@Qualifier` annotation allows you to specify the exact bean to be injected by using its unique identifier. Here’s an example:
“`java
@Service
public class MyService {
@Autowired
@Qualifier(“myRepositoryImpl”)
private MyRepository myRepository;
// …
}
“`
In this example, there are multiple implementations of the `MyRepository` interface in the Spring container. By using `@Qualifier(“myRepositoryImpl”)`, we specify that we want to inject the bean with the identifier “myRepositoryImpl”.
3. @Value:
The `@Value` annotation is used for injecting values from external sources, such as properties files or environment variables, into your Spring components. It allows you to externalize configuration and customize the behavior of your application without modifying the code. Consider the following example:
“`java
@Service
public class MyService {
@Value(“${app.timeout}”)
private int timeout;
// …
}
“`
In this example, the value of the property `app.timeout` is injected into the `timeout` field of the `MyService` class. You can specify the value using a properties file, environment variable, or other external sources.
4. @Primary:
The `@Primary` annotation is used when you have multiple beans of the same type, and you want to specify a default bean to be injected if no qualifier is provided. It is useful when working with interfaces and multiple implementations. Consider the following example:
“`java
@Repository
@Primary
public class DefaultUserRepository implements UserRepository {
// …
}
@Repository
public class CustomUserRepository implements UserRepository {
// …
}
“`
In this example, we have two implementations of the `UserRepository` interface. By annotating `DefaultUserRepository` with `@Primary`, we specify it as the default bean to be injected if no `@Qualifier` is provided.
5. @Lazy:
The `@Lazy` annotation is used to lazily initialize a bean. It delays the initialization of the bean until it is first requested. This can be useful when dealing with expensive resource initialization or cyclic dependencies. Here’s an example:
“`java
@Service
@Lazy
public class MyService {
// …
}
“`
In this example, the `MyService` bean will be lazily initialized, meaning it will only be created when it is first used.
These are just a few examples of the dependency injection annotations available in Spring Boot. Each annotation serves a specific purpose and allows you to easily manage and inject dependencies into your application components. By using these annotations effectively
, you can achieve loose coupling and improve the maintainability and testability of your code.
In the next section, we will explore annotations related to web development, such as request mapping, handling forms, and handling exceptions.
Subscribe to our email newsletter to get the latest posts delivered right to your email.