What to inject – the container itself or just dependencies?

The situation of injecting container occurs when you try to wrap the container itself either in a singleton class or a public static method to provide the dependency to other components or modules, as in the following snippet:

public class AccountService {  //Service method.  public void getVariablePay() {    System.out.println("getting variable pay..");  }}public class HRService {  public int getLeaveInGivenMonth(int monthNo) {    System.out.println(" getting no of leaves for month "+monthNo);    return 2; // just for demo purpose.  }}/* ServiceManager serves like dependency supplier */public class ServiceManager { private static ApplicationContext springContext = new ClassPathXmlApplicationContext("application-context.xml"); ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.