0

Although I have worked in Spring framework for a while, I believe I still don't fully understand it. I want to know how and where dependency injection works.

To reinforce the concepts, I created a standalone Java program, and wanted to see how to inject the implementation using Spring framework.

Here is the snippet of spring bean file:

<beans .....>

    <bean id="CommomImpl" class="com.example.common.impl.CommomImpl">
    </bean>

    <bean id="SimpleImpl" class="com.example.simple.impl.SimpleImpl">
    </bean>
</beans>

Now, the below is the standalone Java program where I wanted to see how DI works.

public class MainApp {

    Resource(name = "SimpleImpl")    -------------------> (1)
    static SimpleTasks simpleTasks1;

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-bean-config.xml");

        SimpleTasks simpleTasks = null;
        CommonTasks commonTasks = null;

        simpleTasks = (SimpleTasks) context.getBean("SimpleImpl"); ----->(2)
        simpleTasks.print("USER");

        commonTasks = (CommonTasks) context.getBean("CommomImpl"); ----->(3)
        commonTasks.add(3,3);

        simpleTasks1.print("USER"); ---------(4)
    }
}

With regards to above program, I have following questions:

Q1: The code at (1) doesn't provide any injection, that is simpleTasks1 is null. What this is so? How come in web-applications, we can inject implementation using similar fashion?

Q2: Are (2) and (3) dependency injection?

Q3: The code at (4) gives null pointer exception, because (1) doesn't inject the implementation. It is this which is confusing me. Under what situations does DI work? When we create the web-application, we deploy the .war inside a web-container and NOT in Spring container, then how come @Resouce(name = "xyz") works, when the spring framework jars itself are provided as dependency to the application.

Any detailed explanation would be of great help to clear these doubts.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134

1 Answers1

1

Dependency injection works by having a way (configuration) to provide dependencies in your classes. Instead of having the class create its dependencies, you pass the required objects from outside. This makes your code testable and you can take advantage of object reusability.

Dependency injection in Spring works in a similar way with other DI frameworks. You can either inject a dependency of a class through its constructor, or through a member field (private fields also work in Spring).

What you're doing is calling the spring context to locate the dependency. Once you do this, Spring will try to locate a bean (provider) for the dependency you want. Once it finds it, it will create the object (by resolving its own dependencies as well) or use an existing instance of it and it will return the instance to you.

Spring provides the @Autowired annotation, as a replacement of using the context, which you can use to inject dependencies in your classes and it makes your code cleaner and safer since you don't use the bean name to inject you dependency.

You could also have a look at Java based configurations for spring to avoid configuring your beans in XML files and use the @Bean annotation.

I would strongly suggest to have a look at the guides in the Spring website, especially the ones that use Spring Boot since they are giving you all the info you need to setup a basic example using Java annotations.

Nick Tsitlakidis
  • 2,269
  • 3
  • 22
  • 26
  • Thanks for your inputs. If possible could you clarify a bit on Q1 and Q4? – CuriousMind Apr 12 '16 at 05:59
  • 1
    Your Resource annotation (and any other similar annotation) will only work if you allow Spring to handle your application lifecycle. If your MainApp is not created by Spring, then there is no way to inject the SimpleTasks automatically. This is why you're getting the null pointer exception. – Nick Tsitlakidis Apr 12 '16 at 07:14
  • Thanks Nick for your reply. My doubt is when we create a web-application and deploy it in a web-container, it is web-container which managers the life cycle of the application, NOT spring. In those case how come @Resource (or any other annotation) work? It is hard for me to visualize this. Any clarity on this, would be of great help. – CuriousMind Apr 12 '16 at 07:26
  • 1
    Spring acts as a web container by implementing and extending the required interfaces and classes. I believe that once you add it properly in your project, it actually handles the lifecycle of your objects. Have a look on this answer and the links it provides too. http://stackoverflow.com/questions/19235436/how-does-the-web-container-manage-the-lifecycle-of-a-spring-controller – Nick Tsitlakidis Apr 12 '16 at 19:38
  • Thanks so much for your reply. The link is helpful! – CuriousMind Apr 13 '16 at 05:51