Skip to main content

Posts

Showing posts with the label Spring dependency injection

Spring Core - Dependency Injection

What is Dependency Injection? Dependency injection is a process in which objects define their dependencies i.e. other objects they require to work, through a constructor, setter methods, factory methods. The container responsibility is to inject those while creating beans. With Dependency inject in place, we have cleaner code and clear way of decoupling. There are two prominent variants of Dependency Injection. Constructor based Dependency Injection Setter based Dependency Injection Constructor based Dependency Injection When you express your dependencies through constructor arguments and your container invoke your constructor with number of arguments, type of arguments expected by the constructor. Let's jump to one quick example. @Component public class ConstructorBasedFileParser { private Parser parser; @Autowired public ConstructorBasedFileParser(Parser parser) { this.parser = parser; } public void setParser(Parser parser) { this.parser = parser;