Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Dependency Injection in REST API: A Guide with Example

Dependency Injection

Dependency Injection (DI) is a design pattern used in software development to achieve Inversion of Control (IoC) between classes and their dependencies. Instead of a class creating its own dependencies, these dependencies are provided by an external source, making the code more modular, testable, and maintainable.

Why Use Dependency Injection?

  • Loose Coupling: DI promotes loose coupling by removing the direct dependency between classes.
  • Improved Testability: Dependencies can be mocked or stubbed easily for unit testing.
  • Simplified Code Management: Makes it easier to manage dependencies in large-scale applications.
  • Enhanced Maintainability: Changing a dependency implementation does not require changes in the dependent class.

Types of Dependency Injection

  • Constructor Injection: Dependencies are provided through a class constructor.
      -> Makes dependencies immutable.
      -> Promotes mandatory dependencies.
  • Setter Injection: Dependencies are provided via setter methods.
      -> Allows optional dependencies.
      -> Useful when a dependency needs to be updated or reconfigured.
  • Field Injection: Dependencies are injected directly into fields (usually using reflection).
      -> Simple and concise.
      -> No need for boilerplate setter or constructor code.

Example: Dependency Injection in Java

Let’s create an example to demonstrate Dependency Injection in a Java application using a basic service.

Example 1: Constructor Injection


@Service
public class UserService {  
	
    private final UserRepository userRepository;
    
    //Constructor Injection
    @Autowired
    public UserService(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public void saveUser(User user) {
        userRepository.save(user);
    }

    public Optional findByUsername(String username) {
        return Optional.ofNullable(userRepository.findByUsername(username));
    }
}

  

Example 2: Setter Injection


@Service
public class UserService {  
	
    private UserRepository userRepository;
    
    //Setter Injection
    @Autowired
    public UserService(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public void saveUser(User user) {
        userRepository.save(user);
    }

    public Optional findByUsername(String username) {
        return Optional.ofNullable(userRepository.findByUsername(username));
    }
}

  

Example 3: Field Injection


@Service
public class UserService {  
	
	//Field Injection
	@Autowired
    private UserRepository userRepository;

	public void saveUser(User user) {
        userRepository.save(user);
    }

    public Optional findByUsername(String username) {
        return Optional.ofNullable(userRepository.findByUsername(username));
    }
}
  

Conclusion

Dependency Injection decouples application components, making them more flexible and maintainable. Whether you use plain Java or a framework like Spring, DI is a powerful pattern to embrace in modern software development. It not only simplifies your code but also prepares it for scaling and testing.

Post a Comment

0 Comments