Spring Core
Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, and reusable code. Spring is lightweight when it comes to size and transparency.
The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promotes good programming practices by enabling a POJO-based programming model.
Enterprise Java refers to Java enterprise software. It’s a computer software used to satisfy the needs of an organization rather than individual users. Examples can include: accounting software, billing Management, business process management, CMS, CRM, ERP, etc.
POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.
Lets’s discuss some of the key terms related to 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.
There are two types of IoC containers:
- BeanFactory
- 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.
A Spring bean is simply a java object. When objects are created by the sppring container, then spring refers to them as spring beans.
The XmlBeanFactory is the implementation class for the BeanFactory interface. To use the BeanFactory, you need to create the instance of XmlBeanFactory class as shown below:

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.
The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. You need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as shown below:

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.
Let’s consider two classes A and B, and say that class A uses functionalities of class B, i.e., class A has a dependency of class B. Now, if you are coding in Java then you must know that, you have to create an instance of class B before the objects are being used by class A. So, if I have to now define Dependency Injection for you, then the process of creating an object for some other class and let the class directly using the dependency is called Dependency Injection.
In Spring, dependencies can be injected in two ways:
- By constructor
- 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
- Create the java project
The first step is to create a simple Java Project
2. Add spring jar files
There are mainly three jar files required to run this application.
- org.springframework.core-3.0.1.RELEASE-A
- com.springsource.org.apache.commons.logging-1.1.1
- org.springframework.beans-3.0.1.RELEASE-A
For the future use, you can download the required jar files for spring core application.
3. Create the class


Following two important points has to be noticed here −
- The first step is to create an application context where we used framework API ClassPathXmlApplicationContext(). This API loads beans configuration file and eventually based on the provided API, it takes care of creating and initializing all the objects, i.e. beans mentioned in the configuration file.
- 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.
4. Create the xml file to provide the values
You need to create a Bean Configuration file which is an XML file and acts as a cement that glues the beans, i.e. the classes together.

When Spring application gets loaded into the memory, Framework makes use of the above configuration file to create all the beans defined and assigns them a unique ID as defined in <bean> tag. You can use <property> tag to pass the values of different variables used at the time of object creation.
5. Create the test class
Once you are done with creating the source and beans configuration files, you are ready for this step, which is compiling and running your program.
