π 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
- ❌
@Autowiredis NOT deprecated, but it's optional since Spring 4.3. - ✅ Best practice: Use constructor injection without
@Autowiredwhenever possible. - π Use
@Autowiredonly when absolutely necessary (e.g., multiple constructors, setter injection).
π‘ Upgrading to Spring Boot 3? Start writing cleaner code by removing unnecessary @Autowired today! π
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment