The spring framework is a lightweight framework used to build scalable, maintainable enterprise applications. It works on the principle of Inversion of Control. (IoC).
What is Inversion of Control?
It refers to the reversal of flow of control in a program. In this the control is shifted to an external entity (the IOC container in case of Spring) that manages the creation and injection of dependencies. The IoC container creates and wires the objects together, allowing objects to focus on their main responsibilities.
In normal flow, a program's custom code calls reusable libraries to take care of generic tasks, but with IoC it is the framework that calls the custom code. In essence, this means delegating execution of tasks to generic code instead of specific code allowing for more versatility. It carries the philosophy that reusable code and problem specific code be developed independently even though they operate together in an application.
Inversion of Control can be achieved through dependency injection, callbacks, schedulers, event loops to name a few methods. We will discuss more deeply on Dependency Injection.
Dependency Injection.
Dependency injection is a design pattern used to remove manual hard coded dependencies in classes by externalizing the creation and management of objects (dependencies).
Instead of an object creating its dependencies itself, it relies on an external entity (the container) to provide the required dependencies. DI promotes loose coupling and improves the flexibility and testability of the code. The dependency injection (DI) pattern is one of the ways to implement IoC.
The Spring container handles all the object instantiation and initialization and injection in the right places.
The Spring Bean is an object that is created and managed by the Spring IoC container.
Thus the Spring IoC container acts like a bag of beans and is responsible for
- Bean creation, maintenance and deletion. - The container reads the bean definitions (from the XML files or annotations), creates the bean instances and manages their life cycles.
The Spring IoC ContainerThe Spring container uses IoC to manage the objects (Spring Beans) that make up an application. The container gets its instructions on what objects to instantiate, configure and assemble by reading the configuration metadata provided. This configuration metadata can be either an XML file, Java annotation or Java code. The container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application.
There are two distinct types of containers that the Spring Framework provides.
- The Spring BeanFactory Container
- The Spring ApplicationContext Container.
The BeanFactory Container - This container provides the basic support for DI and is defined by org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purpose of backward compatibility with a large number of third party frameworks that integrate with Spring.
The key features of the BeanFactory are
- Lazy Initialization : Beans are created only when explicitly requested.
- Lightweight : Minimal memory usage, suitable for resource-constrained environments.
- Basic Bean Management : Supports bean instantiation, configuration, and lifecycle methods like init and destroy.
Example :
// Using BeanFactory (rarely used directly in modern Spring apps)
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
MyBean bean = factory.getBean(MyBean.class);
The ApplicationContext Container - This container adds more enterprise specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface. This container includes all the functionality of the BeanFactory container and so in recommended over BeanFactory container.
God's Word for the day
Hypocrisy and Retribution
Whoever winks the eye plans mischief
and those who know him will keep their distance.
In your presence his mouth is all sweetness
and he admires your words;
But later he will twist his speech, and with your
own words he will trip you up.
I have hated many things but him above all;
even the Lord hates him
Whoever throws a stone straight up throws it on his own head
and a treacherous blow opens up many wounds.
Whoever digs a pit will fall into it,
and whoever sets a snare will be caught in it.
If a person does evil it will rollback on him
and he will not know where it came from
Mockery and abuse issue from the proud,
but vengeance lies in wait for them like a lion.
Those who rejoice in the fall of the godly will be caught in a snare
and pain will consume them before their death.
Sirach 27:22 - 29
Gospel teachings of Jesus
The laborers in the vineyard
For the kingdom of heaven is like a landowner who went out
early in the morning to hire laborers for his vineyard.
After agreeing with the laborers for the usual daily wage,
he sent them into his vineyard. When he went out about
nine O' clock, he saw others standing idle in the marketplace.
and he said to them, 'You also go into the vineyard and I will
pay you whatever is right. So they went. When he went out
again about noon and about 3 O 'clock he did the same. And
about five O'clock he went out and saw others standing around,
and he said to them, 'Why are you standing here idle all day?'
And they said to him, 'Because no one has hired us.'
He said to them, 'You also go into the vineyard.' When evening
came the owner of the vineyard said to the manager, 'Call the
laborers and give them their pay, beginning from the last and
going to the first.' When those hired about five O' clock each of
them received the usual daily wage. Now when the first came
they thought they would receive more; but each of them received
the usual daily wage. Now when the first came
Mathew 19:27 - 30