Spring Core

IOC Container

The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The IoC container gets informations from the XML file and works accordingly.

  • ApplicationContext

BeanFactory

Bean Factory provides the basic support for Dependency Injection. BeanFactory follows lazy-initialization technique which means beans are loaded as soon as bean factory instance is created but the beans are created only when getBean() method is called.

ApplicationContext

It is an interface defined in “org.springframework.context.ApplicationContext”. It is the central interface within a spring application that is used for providing configuration information to the application. This implements the BeanFactory interface. Hence, the ApplicationContext includes all functionality of the BeanFactory. Its main funtion is to support the creation of ig business applications.

Dependency Injection

Dependency Injection is one such technique which aims to help the developer code easily by providing dependencies of another object. Dependency Injection is also one of the core concepts of Spring Framework. It is a design pattern that removes the dependency from the code. That is, the Spring Framework provides the dependencies of the class itself so that it can be easy to manage and test the application. You can provide information from external source such as XML file. Here, you do not create the objects instead you just define how they should be created and IoC container will create the objects for you.

  • By setter method

By constructor

The <constructor-arg> subelement of <bean> is used for constructor injection.

By setter method

The <property> subelement of <bean> is used for setter injection.

Beans Scope: SIngleton vs Prototype

When defining a <bean> you have the option of declaring a scope for that bean. For example, to force Spring to produce a new bean instance each time one is needed, you should declare the bean’s scope attribute to be prototype. Similarly, if you want Spring to return the same bean instance each time one is needed, you should declare the bean’s scope attribute to be singleton.

Steps to create simple Spring Application

  1. Create the java project
  • com.springsource.org.apache.commons.logging-1.1.1
  • org.springframework.beans-3.0.1.RELEASE-A
HelloWorld.java
MainPage.java
  • The second step is used to get the required bean using getBean() method of the created context. This method uses bean ID to return a generic object, which finally can be casted to the actual object. Once you have an object, you can use this object to call any class method.
Output

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store