Java Spring

The current stable version, as of December 2022, is Spring 6.0.2.
The current stable version, as of December 2022, is Spring Boot 3.0.0
What's New in Spring Framework 6.x
Spring boot 3 release
Feature Name Description
What is Spring Spring is the most broadly used lightweight, loosely coupled and integrated framework for the development of Java Enterprise Edition applications.
  • Lightweight – There is a slight overhead of using the framework in development.
  • Inversion of Control (IoC) – Spring container takes care of wiring dependencies of various objects instead of creating or looking for dependent objects.
  • Aspect-Oriented Programming (AOP) – Spring supports AOP to separate business logic from system services.
  • IoC container – manages Spring Bean life cycle and project-specific configurations
  • MVC framework – used to create web applications or RESTful web services, capable of returning XML/JSON responses
  • Transaction management – reduces the amount of boilerplate code in JDBC operations, file uploading, etc., either by using Java annotations or by Spring Bean XML configuration file
  • Exception Handling – Spring provides a convenient API for translating technology-specific exceptions into unchecked exceptions.
Spring Sub-Projects
  • Core – a key module that provides fundamental parts of the framework, such as IoC or DI
  • JDBC – enables a JDBC-abstraction layer that removes the need to do JDBC coding for specific vendor databases
  • ORM integration – provides integration layers for popular object-relational mapping APIs, such as JPA, JDO and Hibernate
  • Web – a web-oriented integration module that provides multipart file upload, Servlet listeners and web-oriented application context functionalities
  • MVC framework – a web module implementing the Model View Controller design pattern
  • AOP module – aspect-oriented programming implementation allowing the definition of clean method-interceptors and pointcuts
Types of Dependency Injection
  • Setter injection - support partial injection, can override constructor, better for few properties
  • Constructor injection - better for mandatory dependencies
  • Field injection - use fields with @Autowired
Containers: BeanFactory and ApplicationContext BeanFactory - basic container, is an interface representing a container that provides and manages bean instances. The default implementation instantiates beans lazily when getBean() is called.
In contrast, ApplicationContext - advanced container is an interface representing a container holding all information, metadata and beans in the application. It also extends the BeanFactory interface, but the default implementation instantiates beans eagerly when the application starts. However, this behavior can be overridden for individual beans.
Bean Scopes

singleton

Scopes a single bean definition to a single object instance per Spring IoC container. Stateless

prototype

Scopes a single bean definition to any number of object instances. New instance by each dependency. Stateful

request

Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

Singleton beans with prototype-bean dependencies When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.
Are Singleton Beans Thread-Safe? No, singleton beans are not thread-safe, as thread safety is about execution, whereas the singleton is a design pattern focusing on creation. Thread safety depends only on the bean implementation itself.
What are the autowiring modes?
Autowiring enables the programmer to inject the bean automatically. We don't need to write explicit injection logic.
1)nothis is the default mode, it means autowiring is not enabled.
2)byNameinjects the bean based on the property name. It uses setter method.
3)byTypeinjects the bean based on the property type. It uses setter method.
4)constructorIt injects the bean using constructor
Spring Bean Life Cycle