instantiate dependencies manually, usually inside constructor. For example:
public class UserService {
private UserRepository userRepository;
public UserService() {
this.userRepository = new UserRepositoryImpl();
}
}
Well this may look simple, right? But what if your component has three dependencies, and those dependencies also have their own dependencies? So you have to write something like this in your constructor:
this.userRepository = new UserRepositoryImpl(new EntityManager());
this.someOtherDepenpency = new SomeOtherDependency(new ExternalDependencyOne(), new ExternalDependencyTwo(new ExternalDependencyThree()));
// etc
This will quickly become a headache as your application becomes more complex
so how DI can help me i this case the example in the end
Обсуждают сегодня