What is Spring bean?

In short, a Spring bean is an object which Spring framework manages at runtime. A Spring bean is a basic building block of any Spring application. Most of the application logic code you write will be placed in Spring beans.

The management of a Spring bean includes:

  • creating an object
  • providing dependencies (e.g. other beans, configuration properties)
  • intercepting object method calls to provide additional framework features
  • destroying an object

A Spring bean is a fundamental concept of the framework. As a user of Spring, you should have a deep understanding of this core abstraction.

Keep reading and you’ll find out all you need to know about Spring beans.
Continue reading “What is Spring bean?”

Injecting Spring Prototype bean into Singleton bean

Have you ever wonder why singleton is the default scope for Spring beans? Why isn’t it prototype?

It’s not a random choice. It’s because the vast majority of business logic we create can be safely kept in stateless objects. And the best choice for stateless beans is the singleton scope. The prototype scope is better for stateful beans to avoid multithreading issues.

Yet, sometimes you need to mix both and use a prototype bean in a singleton bean. This particular case is a bit tricky. In this article, I’m going to explain to you different ways of accessing prototypes in singletons.

So let’s begin.

Continue reading “Injecting Spring Prototype bean into Singleton bean”

Spring bean thread safety guide

Is Spring controller/service/singleton thread-safe?

It’s a commonly asked question by Spring newcomers and probably a must-have warm-up question on job interviews. As usual in programming, the answer is: it depends. The main factor which determines thread safety of a component is its scope.

Let’s get down to it and see what Spring’s scopes have to offer in multithreaded programming.

Continue reading “Spring bean thread safety guide”