Get ready for your next interview with our comprehensive question library
IoC is a design principle where the control of object creation and management is transferred from the application code to an external container (Spring Container).
DI is the implementation of IoC where dependencies are injected into objects rather than objects creating their dependencies.
// Without DI - tight coupling
public class UserService {
private UserRepository userRepository = new UserRepository(); // Tight coupling
}
// With DI - loose coupling
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository; // Dependency injected
}
}
Spring supports three types of DI:
// Constructor Injection (Recommended)
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
// Setter Injection
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
// Field Injection
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
}
These are stereotype annotations for different layers:
All are essentially @Component with semantic meaning. @Repository provides additional exception translation.
Spring supports three configuration approaches:
// Java-based configuration
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService(userRepository());
}
@Bean
public UserRepository userRepository() {
return new UserRepository();
}
}
@Autowired enables automatic dependency injection. Spring automatically injects matching beans by type. It can be used on:
@Service
public class UserService {
@Autowired
private UserRepository userRepository; // Field injection
@Autowired
public UserService(UserRepository userRepository) { // Constructor injection
this.userRepository = userRepository;
}
}
Spring Boot is an opinionated framework that simplifies Spring application development by providing:
Starters are pre-configured dependency descriptors that include all necessary dependencies for specific functionality:
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime