What are we going to learn ?We will look at what exactly Spring is and what it provides. We will also look at Dependency Injection and Inversion Of control.
What is Spring ?
Spring is an open source framework and Inversion Of Control container. Spring provides comprehensive infrastructure support for developing Java applications. JEE applications are complex and multi layered. Spring provides the capability to develop all layers of application using Spring API's. In contrast to single layered frameworks like Struts/hibernate, Spring is not invasive and not "all or nothing" framework. Spring acts like a pluggable framework where you can switch between different implementations.
Typically, an application consists of components interacting and collaborating with one another. Spring provides you the capability to build applications from POJOs( Plain Old Java Objects ) and apply enterprise services non-invasively.
Dependency injection (DI) and Inversion Of Control(IOC):
Dependency Injection is a software pattern that removes hard-coded dependencies. As explained earlier, applications involve collaboration between different components.
Let's consider a small example to consider the advantages of using DI. Let's say you are creating a Student enrollment application and you would like to use log4j to log the functional behavior. Under normal circumstances, you create a logger class and use it for logging. Some of the disadvantages of the approach being
In DI, the onus of providing the reference of the required object/dependency and maintaining it's life cycle falls on the IOC Container.
You mentioned Spring as Inversion Of Control container. What is it's functionality?
In Spring, the objects are the backbone of the application. These objects managed by the Spring IOC are called beans. Spring IOC is responsible for instantiating, configuring and assembling of the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich inter-dependencies between such objects.
How is the configuration metadata provided to Spring IOC Container or how is the Spring IOC container instantiated?
It's very important that you understand how Spring IOC functions. Following will help you understand the inner working of the Spring IOC.
The interface
org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich interdependencies between such objects.
Several implementations of the
ApplicationContext interface are supplied out-of-the-box with Spring. In standalone applications it is common to create an instance ofClassPathXmlApplicationContext or FileSystemXmlApplicationContext.
What are the different ways for DI in Spring?
When does spring create the bean ?
The Spring container validates the configuration of each bean as the container is created, including the validation of whether bean reference properties refer to valid beans. However, the bean properties themselves are not set until the bean is actually created. Beans that are singleton-scoped and set to be pre-instantiated (the default) are created when the container is created.Otherwise the bean and it's dependencies are created when the bean is requested.
Spring supports 5 different scopes, three are available for only web-aware ApplicationContext.
References :
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/htmlsingle/
|