The Spring Framework is an open source application framework and inversion of control container for the Java platform.
Spring has made it so that configuring an ApplicationContext
is extremely flexible. There are numerous ways to apply each type of configuration, and they can all be mixed and matched together nicely.
Java configuration is a form of explicit configuration. A @Configuration
annotated class is used to specify the beans that will be a part of the ApplicationContext
, as well as define and wire the dependencies of each bean.
Xml configuration is a form of explicit configuration. A specific xml schema is used to define the beans that will be a part of the ApplicationContext
. This same schema is used to define and wire the dependencies of each bean.
Autowiring is a form of automatic configuration. Certain annotations are used in class definitions to establish what beans will be a part of the ApplicationContext
, and other annotations are used to wire the dependencies of these beans.
Important point to note while using condition
@Value
property injection i.e. no other spring beans can be injected within it.BeanFactoryPostProcessor
and take care to never interact with bean instances. The restrictions refereed here are A BeanFactoryPostProcessor
may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects.The source code for large software applications is typically organized into multiple units. The definition of a unit normally varies by the programming language used. For example, code written in a procedural programming language (like C) is organized into functions
or procedures
. Similarly, code in an object-oriented programming language (like Java, Scala and C#) is organized into classes
, interfaces
and so on. These units of code organization can be thought of as individual units making up the overall software application.
When applications have multiple units, inter-dependencies between those units crop up when one unit has to use others to complete its functionality. The dependent units can be thought of as consumers
and the units on which they depend upon as providers
of specific functionality.
The easiest programming approach is for the consumers to fully control the flow of a software application by deciding which providers should be instantiated, used and destroyed at what points in the overall execution of the application. The consumers are said to have full control over the providers during the execution flow, which are dependencies
for the consumers. In case the providers have their own dependencies, the consumers may have to worry about how the providers should be initialized (and released), making the control flow more and more complicated as the number of units in the software goes up. This approach also increases the coupling between units, making it increasingly difficult to change units individually without worrying about breaking others parts of the software.
Inversion of Control (IoC) is a design-principle that advocates outsourcing control flow activities like unit discovery, instantiation and destruction to a framework independent of the consumers and providers. The underlying principle behind IoC is to decouple consumers and providers, freeing software units from having to worry about discovering, instantiating and cleaning up their dependencies and allowing units to focus on their own functionality. This decoupling helps keep the software extensible and maintainable.
Dependency injection is one of the techniques for implementing the inversion of control principle whereby instances of dependencies (providers) are injected into a software unit (the consumer) instead of the consumer having to find and instantiate them.
The Spring framework contains a dependency injection module at its core which allows Spring-managed beans to be injected into other Spring-managed beans as dependencies.