Skip to content

Instantly share code, notes, and snippets.

@edwardoboh
Last active July 16, 2023 09:39
Show Gist options
  • Save edwardoboh/1a8c9d3c77515b310842137026eb05c7 to your computer and use it in GitHub Desktop.
Save edwardoboh/1a8c9d3c77515b310842137026eb05c7 to your computer and use it in GitHub Desktop.
My Notes on Spring Framework and Spring Boot

Spring Notes

Coupling is measured by the amount of work that needs to be done whenever a change needs to be made.

Spring Beans is any Java object managed by the Spring framework


The IoC container manages the lifecycle of beans and dependencies. It comprises:

  • Application Context: complex and most widely used
  • Bean Factory: Simple and rarely used

Dependency Injection in the Spring Framework comprises:

  • Constructor injection: Where dependencies are injected throw constructor parameters into the Bean. Using the @Autowired or @Inject Annotation here is optional.
  • Setter injection: Where dependencies are injected through parameters of setter methods of fields (instance variables) into the Bean class. Here we Annotate the method with @Autowired.
  • Field Injection: Here, Spring Framework injects dependencies using Reflection. Used when there are no Constructors or Setters. We make use of the @Autowired annotation.

Spring allows for Lazy and Eager loading of Beans. Eager Loading is the default;

  • Eager Loading: Default behavious. Beans created during application Initialization/boostraping phase.
  • Lazy Loading: Using @Lazy or @Lazy(value=true); Bean is created during runtime when it is needed and not at application bootstraping. Not recommended as error with Bean configuration at not caught at application start time but at runtime.

Varios Bean Scopes in Spring Framework

  • Singleton: Returns the same Bean instance whenever a particular Bean type is required; The default behaviour; @Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)
  • Prototype: Returns a new instance of the Bean type whenever it is required; @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  • Web-aware Scopes:
    • Request
    • Session
    • Application
    • Web Socket

The difference between Java Singleton and Spring Singleton

  • Java Singleton typically refers to having a single instance of an Object in the JVM used across an application
  • Spring Singleton on the other hand refers to having a single instance of an object per IoC container Both coucld mean the same thing if we are running one Spring container per JVM.

In most cases, one IoC container is used, which somewhat makes Java Singleton and Spring Singleton the same thing

Autowiring is the process of wiring dependencies for a Spring Beans


Post Construct and Pre Destroy Hooks

  • @PostConstruct: This annotation is used on a method that needs to be executed after dependency injection in done, to perform any initialization. It must be invoked before the class is put into service.
  • @PreDestroy: This annotation is used on a method as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with PreDestroy is typically used to release resources that it has been holding.

The @Named annotation (from jarkata.inject.Named) is an alternative to @Component

Can also use the @Inject annotation (from jarkata.inject.Inject) in place of @Autowired

Other annotations from Jarkata are: @Scope, @Qualifier and @Singleton

The Jarkata CDI (Context and Dependency Injection) is just a specification defining a set of Annotations. There are no actual implementations. Spring Framework implements this specifications.


Other Stereotype Annotations in Spring:

Note that the @Component Annotation is the base for other Annotations.

  • @Service: Used when a Bean class contains business logic
  • @Controller: Used when the Bean class is a web controller. Used in web applications and APIs
  • @Repository: Used when the Bean class talks to a Database; Retrieving and Manipulating Data.

All Annotations so far:

@Configuration, @Bean, @Component, @ComponentScan, @Autowired, @Primary, @Qualifier, @Named, @Inject, @Service, @Controller, @Repository, @Lazy, @Scope, @PostConstruct, @PreDestroy, @Singleton


Domains:

  • Spring Framework
  • Spring Module:
  • Spring Projects:
    • First Project (Spring Framework)
    • Spring Boot
    • Spring Data
    • Spring Security
    • Spring Cloud
    • Spring Integration

Hierarchy:

Spring Projects > Spring Framework > Spring Modules


In Spring, there is Java configurations (the default with modern spring) and XML configuration (was used in earlier versions of spring)


Questions

Q1. Describe the difference between Dipendency Inversion and Inversion of Control

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment