Spring

Vaibhav Bharadwaj
3 min readDec 14, 2020

What is Spring Framework?

It is a very lightweight framework which provides well-defined infrastructure support for developing Java application. In other words, it handles the infrastructure so, that you can focus more on developing your application.

Dependency Injection

Java Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable. We can implement dependency injection in java to move the dependency resolution from compile-time to runtime.

Inversion of Control (IoC)

Inside Spring, a bean exploits the Inversion of Control feature by which an object defines its dependencies without creating them. This object delegates the job of constructing and instantiating such dependencies to an IoC container, the Spring lightweight container.

Aspect-Oriented Programming

Aspect-Oriented Programming (AOP) allows us to separate the (usually duplicated and boilerplate) profiling code from the service code.

Model View and Controller

It is a design pattern that separates the business logic, presentation logic and data.

Model represents the state of the application i.e. data. It can also have business logic.

Controller acts as an interface between View and Model. Controller intercepts all the incoming requests.

View represents the presentation i.e. UI(User Interface).

Spring Core Container

The Spring Core container contains core, beans, context and expression language (EL) modules.

Bean

Spring bean is an object that form the backbone of your application and that is managed by the Spring IoC container. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Bean annotations

  1. @Component : class-level annotation which by default denotes a bean with the same name as the class name with a lowercase first letter. It can be specified a different name using the value argument of the annotation
  2. @Repository : class-level annotation representing the DAO layer of an application; it enables an automatic persistence exception translation.
  3. @Service : class-level annotation representing where the business logic usually resides and can flavor the reuse of the code.
  4. @Controller : class-level annotation representing the controllers in Spring MVC (Model-View-Controller). See also @RestController for the REST “mode”.
  5. @Configuration : class-level annotation to say that it can contain bean definition methods annotated with @Bean

Bean Lifecycle

Spring Bean factory is responsible for managing the lifecycle of beans created through the Spring container and controls the creation and destruction of the beans. To execute some custom code, it provides the call back methods which can be categorized broadly into two groups:

  • Post-initialization callback methods
  • Pre-destruction callback methods

Spring framework provides the following 4 ways for controlling the life cycle events of a bean:

  • InitializingBean and DisposableBean callback interfaces
  • Aware interfaces for specific behavior (BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ApplicationContextAware)
  • custom init() and destroy() methods in bean configuration file
  • @PostConstruct and @PreDestroy annotations

Context

Spring contexts are also called Spring IoC containers, which are responsible for instantiating, configuring, and assembling beans by reading configuration metadata from XML, Java annotations, and/or Java code in the configuration files.

Expression Language

The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime. It can be used with XML or annotation-based Spring configurations.

--

--