Spring Core Introduction#
Spring is one of the most popular frameworks for Java enterprise edition. Developers use Spring for developing reliable and high-quality applications. Spring was designed by Rob Johnson.
Spring Core Concepts#
Inversion of control
: This is the principle of object-oriented programming, in which objects of the program do not depend on concrete implementations of other objects, but may have knowledge about their abstractions (interfaces
) for later interaction.Dependency Injection
: is a composition of structural design patterns, in which each function of the application there is one, a conditionally independent object (service
) that can have the need to use other objects (dependencies
) know to it by interfaces. Dependencies are transferred (implemented
) to the service at the time of it's creation. This is the situation where we introduce an element of one class into another. In practice, DI is implemented to the constructor or using setters. Libraries that implement this approach are also calledIoc containers
.Aspect Oriented Programming
: a programming paradigm that allows to distinguish cross-through (functional
) functionality in application. These functions, which spam multiple application nodes, are callcross-cutting concerns
and thesecross-cutting nodes
are seperated from the immediate business logic of the application. In OOP, the key unit is the class, which in AOP, the key element is theaspect
. DI helps to separate application classes into separate modules, and APO helps to createcross-cutting concerns
from the objects they effect.
Spring IoC Container#
Spring IoC Container
is the machanism to achieve loose-coupling between Object dependencies. To achieve loose coupling and dynamic binding of the objects at runtime, objects dependencies. Spring IoC container is the program that injects dependencies into an object and make it ready for our use.Spring IoC Container
classes are part oforg.springframework.bean
andorg.springframework.context
packages.BeanFactory
is the root interface of spring IoC container.ApplicationContext
is the child interface ofBeanFactory
.- Usually, if you are working with Spring MVC application and your application is configured to use Spring Framework, Spring IoC container gets initialized when the application starts and when a bean is requested, the dependencies are injected automatically.
Spring Bean#
Spring Bean
is nothing special, any object in the Spring Framework that initialize through spring container is callSpring Bean
. Any normal Java POJO class (Plain Old Java Object) can be aSpring Bean
if it is configured to be initialized via container by providing configuration metadata information.
Spring Bean Scopes#
- There are 5 scopes definded for
spring beans
:singleton
: Only one instance of the bean will be create for each container. This is the default scope of spring beans. While using this scope, make sure bean does not have shared instances variables otherwise it might lead to data insconsistency issues.prototype
: a new instance will be created every time the bean is requested.request
: This is the same as prototype scope, however it meant to be used for web application. A new instance of the bean will be created for each HTTP request.session
: A new bean will be created for each HTTP session by the container.global-session
: This is used to create global session bean for Prolet applications.
See Also#
- Spring Boot Introduction
- Spring Data Introduction
- Spring Cloud Introduction
- Spring Security Introduction