π Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
π 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! π
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment