📌 Is @Autowired
Deprecated in Spring Boot?
No, @Autowired
is NOT deprecated in Spring Boot. However, since Spring 4.3, it has been made optional if a class has only one constructor.
With Spring Boot 3+, the best practice is to use constructor injection without @Autowired
, as Spring automatically injects dependencies when a bean has only one constructor.
🔹 What is @Autowired
?
The @Autowired
annotation is used in Spring to enable automatic dependency injection. It allows Spring to inject the required dependencies into a class automatically, without requiring manual object creation.
✅ Example: Using @Autowired
to Inject Dependencies
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> getAllProducts() {
return productRepository.findAll();
}
}
💡 This tells Spring to automatically inject an instance of ProductRepository
into ProductService
.
📌 Why is @Autowired
Optional in Spring Boot 3+?
Since Spring 4.3, @Autowired
is not needed when using constructor-based injection, provided that there is only one constructor in the class.
✅ How Spring Automatically Injects Dependencies
If a class has a single constructor, Spring automatically injects dependencies without requiring @Autowired
.
🚨 Before (Using @Autowired
)
@Service
public class ProductService {
private final ProductRepository productRepository;
@Autowired // Not needed in Spring Boot 3+
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
}
✅ After (Best Practice)
@Service
public class ProductService {
private final ProductRepository productRepository;
// Spring automatically injects this dependency!
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
}
📌 Why is this better?
- Removes unnecessary annotations → Cleaner code
- Encourages immutability → Dependencies can be
final
- Improves testability → Easier to mock dependencies
📌 When Do You Still Need @Autowired
?
Even though @Autowired
is optional, there are some cases where you still need it:
1️⃣ If There Are Multiple Constructors
If a class has multiple constructors, you must explicitly tell Spring which one to use by adding @Autowired
to the desired constructor.
@Service
public class OrderService {
private final PaymentService paymentService;
private final NotificationService notificationService;
@Autowired
public OrderService(PaymentService paymentService, NotificationService notificationService) {
this.paymentService = paymentService;
this.notificationService = notificationService;
}
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
this.notificationService = null; // Default behavior
}
}
✅ Spring will inject dependencies into the @Autowired
constructor.
2️⃣ When Using Field Injection (NOT Recommended)
If you use field injection, you must use @Autowired
because Spring won't know where to inject the dependency.
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
}
📌 Why is field injection NOT recommended?
❌ Harder to test (no easy way to mock dependencies).
❌ Less maintainable and less readable code.
✅ Use constructor injection instead!
3️⃣ When Using Setter Injection
If you use setter injection, @Autowired
is required.
@Service
public class CustomerService {
private CustomerRepository customerRepository;
@Autowired
public void setCustomerRepository(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
}
📌 Setter injection is useful when dependencies are optional.
4️⃣ When Using @Bean
Method Injection
If you define a bean manually using a @Bean
method, @Autowired
is not needed, because Spring automatically injects dependencies.
@Configuration
public class AppConfig {
@Bean
public ProductService productService(ProductRepository productRepository) {
return new ProductService(productRepository);
}
}
✅ Spring automatically injects ProductRepository
into ProductService
.
📌 Should You Stop Using @Autowired
?
You don’t need @Autowired
in most cases if you use constructor-based injection in Spring Boot 3+.
✅ Best practice: Use constructor injection without @Autowired
for clean, maintainable, and testable code.
✅ Still needed? Yes, for multiple constructors, setter injection, or field injection.
🚀 Conclusion
- ❌
@Autowired
is NOT deprecated, but it's optional since Spring 4.3. - ✅ Best practice: Use constructor injection without
@Autowired
whenever possible. - 🔄 Use
@Autowired
only when absolutely necessary (e.g., multiple constructors, setter injection).
💡 Upgrading to Spring Boot 3? Start writing cleaner code by removing unnecessary @Autowired
today! 🚀
Comments
Post a Comment
Leave Comment