Spring Principles
Spring is based on 2 core principles :
- Dependency Injection
- Inversion of Control
Understanding of application development
Where Spring Framework fits ?
Let’s take an example of an application using 3-layered architecture.
If the Business layer needs some data, it calls some class which is present inside the data layer. So, here the business layer is dependent on the data layer. Similarly, if we want to show some data to the user or save some data entered by the user, the web layer needs to talk to the business layer. This is nothing but, the web layer is dependent on some class which is present inside the business layer.
A class which needs the service of another class to provide certain functionality, is said to be dependent on another class.[this is just that, what are all things or classes it needs to perform its own functionality.]
Spring Framework – is about create/instantiate the objects and populate the dependencies.
It is your job as a programmer to tell Spring Framework what are the objects it would need to manage and what are the dependencies of each class.
Spring Framework was created by Rod Johnson in the year 2003.
One of the important things about Spring Framework is that it’s not one big Framework. It’s not like you have to use the entire Framework or you don’t use it at all. It’s not one big JAR file present. Lots of smaller JAR files, each for different purposes.
Spring is built in a very modular way and this enables you to use specific modules without using other modules of the Spring Framework.
Spring Modules have the same release version as the Spring Framework.
Spring Projects provide solutions for different problems faced by enterprise applications. Like, Spring Batch, Spring Security, Spring Boot, Spring Cloud, etc.
- Spring is a complete and a modular framework, I mean spring framework can be used for all layer implementations for a real time application(multi-layered architecture) or spring can be used for the development of particular layer of a real time application unlike struts [ only for front end related ] and hibernate [ only for database related ], but with spring we can develop all layers.
-> UI Layer –> Struts/JSF Integration, Spring MVC
-> Service/Business Layer –> Spring REST, Spring Security, Transaction Mgmt
-> DB Layer –> Spring Data [ Spring JDBC, Spring ORM]
- Spring framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their classes from any predefined class or interface given by Spring API.
- Spring is lightweight framework because of its POJO model.
It was created :
- to enhance developer productivity
- to enforce some of the industry’s well-known best coding practices.
Loose Coupling :
In spring objects are loosely coupled, this is the core concept of spring framework. We will see in depth about this loose coupling and how it differs from tight coupling later.
The core of Spring Framework is the ‘Inversion of Control’ container or IoC container.
IoC container provides :
- consistent means of configuring and managing Java objects.
- IoC containers are responsible for managing these objects’ life cycle.
- Creating those objects
- Calling their initialization methods
- Configuring these objects by wiring them together.
Spring comes with a great deal of default behavior already implemented. Components called “infrastructure beans” have a default configuration that can be used or easily customized to fit the project’s requirements.
Spring is built on the principle “Convention over Configuration”.
Spring official Javadoc: http://docs.spring.io/spring/docs/current/javadoc-api/
Spring Reference: http://docs.spring.io/spring/docs/current/spring-framework-reference/
Dependency Injection
Dependency injection is the process of injecting what an object requires at run time or dynamically.
Dependency Injection (DI) is all about injecting a class’s dependencies into it at runtime. This is based on the “Dependency Inversion Principle” by defining the dependencies as interfaces, and then injecting in a concrete class implementing that interface to the constructor (or via setter methods). This allows you to swap different implementations without having to make any structural changes.
It is a design pattern where instead of having your objects create a dependency or asking a factory object to make one for you, you pass the needed dependencies into the constructor (i.e. Constructor Injection) or via setter methods (i.e. Setter Injection) from outside the class.
This is achieved by defining the dependencies as interfaces, and then injecting in a concrete class implementing that interface via a constructor (i.e. constructor injection) or a setter method (i.e. setter injection) by wiring up via an IoC container like Spring. You can wire up the dependencies from outside using an XML config file or using annotations. Dependency Injection is a design pattern that allows us to write loosely coupled code for better maintainability.
Inversion of Control
It is a software design principle where the framework controls the program flow. Spring framework, Guice, etc are IoC containers that implement the IoC principle.
The core of Spring Framework is IoC container. What are you “Inverting” in IoC?
“The process of moving away control of object creation from application code to an external framework is called Inversion of Control.”
Flow of control is “inverted” by dependency injection because you are effectively delegating dependencies to some external system (e.g. IoC container like Spring)
The core of the Spring Framework is its Inversion of Control (Ioc) container. An IoC container like Spring is responsible for loosely wiring up the dependencies. When Spring application runs, it looks at the either XML config file or the annotations to wire up the dependencies.
- The Spring IoC container manages Java objects from their instantiation to destruction via its BeanFactory.
- Java components that are instantiated by the IoC container are called beans, and
- The IoC container manages a bean’s scope (e.g. prototype vs singleton), lifecycle events (e.g. initialization, method callbacks & shutdown), and any AOP (Aspect Oriented Programming) features if configured.
The basic concept of the Inversion of Control pattern is that you do not create your objects but describe how they should be created.
You don’t directly connect your components and services together in the code, but describe which services are needed by which components via configuration files in XML/Java and annotations. The IoC container is then responsible for hooking it all up.
The objects are given their dependencies at creation time by some external entity that coordinates each object in the system.