Have you ever wondered how many ‘beans’ your project uses behind the scenes?
This article will focus on calculating the number of Beans, the silent, behind-the-scenes magical ingredients that we frequently use in our projects. We’ll embark on an exhilarating journey to uncover the number of beans that are hidden in your codebase.
Before we continue with the article, I recommend having a basic understanding of what Beans are. If you already have sufficient knowledge, feel free to proceed. However, if not, you can access my previous article by clicking on this link or refer to the documentation by clicking here.
I would also like to thank my teacher, who has always been supportive and inspiring throughout the process of writing this article and the previous ones. While writing this article, I benefited from his writing as well. I am adding the link to the article here.
To briefly explain beans, they are created and managed by the Spring container, are marked with annotations, and are typically represented as Java classes. Beans are marked with annotations provided by Spring, such as @Component, @Service, @Repository, and are typically injected using @Autowired. Now, let’s go back to our main issue…
Let’s get started if your coffee is ready!
Behind the scenes, these magical ingredients are working quietly. But how many of them are running?
We can solve this problem in three different ways:
- Using Spring Actuator
- With the help of the Application Context
- Through IDEs
In this article, we’ll reach the solution using Spring Actuator and Application Context.
a)Using Spring Boot Actuator
By using Actuator with Spring Boot, we can simplify our tasks. Before proceeding further, it would be beneficial to briefly explain the difference between Spring and Spring Boot for better understanding.
Spring vs Spring Boot
- Spring: Best for complex enterprise web apps, serverless, microservices, event-driven architecture, high-security needs, and asynchronous code.
- Spring Boot: Ideal for standalone app development and quickly creating production-ready Spring applications.
Spring manages classes by allowing users to select what they need, while Spring Boot handles much of the background work automatically. To illustrate, consider having fruits at hand. Spring would want us to pick out each fruit ourselves, whereas Spring Boot already does this in the background.
I mentioned Spring Boot at the beginning because the main focus, Spring Actuator, is a module provided by Spring Boot. We needed to know this information before proceeding further.
Now, back to our topic. We mentioned that we can find out how many beans are running in our application through various methods, one of which is Spring Actuator. However, it’s worth adding further explanation. Actuator not only helps show how many beans there are but also brings production-ready features to our application. The actuator mainly exposes operational information about the running application:
- Health, metrics, info, dump, env, etc. (It uses HTTP endpoints or JMX beans to enable us to interact with it.)
By leveraging Actuator’s endpoint, we can uncover the secrets of our beans. We can use the /beans
endpoint to retrieve information about the beans in our application.
Let’s examine it step by step
1-Add Spring Boot Actuator Dependency:
Make sure you have the spring-boot-starter-actuator
dependency in your pom.xml
or build.gradle
file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2-Enable Actuator Endpoints:
In your application.properties
or application.yml
file, enable the Actuator endpoints.
3-Access /beans
Endpoint:
After running your Spring Boot application, you can access the /beans
endpoint to retrieve information about the beans.
We can see that when we make a request in Postman, all beans are listed as shown in the image below.
Our goal from the beginning has been to find the number of beans. Now, let’s use the small project we’ve written to calculate how many beans are working.
Let’s examine the beans and print the actual numbers of beans…
In this way, by sending a request through Postman to our endpoint, we were able to determine the number of beans.
Let’s look at our next method.
b)Using Application Context
1-Adding the Necessary Dependencies:
2-Creating Spring Boot Application:
3-Creating a Service to Calculate Bean Count Using Application Context:
ApplicationContext
bean using constructor injection. Then, we define a method countBeans()
that simply returns the count of all beans defined within the application context using applicationContext.getBeanDefinitionCount()
.This approach allows you to directly access the bean count without relying on the Spring Boot Actuator. This approach is a common way to create RESTful services in Spring Boot projects and is often used to expose data to the outside world.
Can we consider a different approach?
In this combined class:
- We removed the
BeanCounterService
class as we integrated its functionality into theBeanCounter
class. - The
BeanCounter
class implementsCommandLineRunner
, allowing it to run therun()
method when the application starts. - Inside the
run()
method, we call thecountBeans()
method to get the bean count and print it to the console.
To sum up, the BeanCounterService
is a service class designed to be called from any point in the application to retrieve and return the bean count. On the other hand, BeanCounter
is a class that executes only once when the application starts and its sole purpose is to print the bean count to the console. These two components have distinct roles and serve different purposes within the application.
Sharing what I have learned is something I enjoy as a computer engineering student. I’d appreciate it if you could provide feedback to support me and assist me in revisiting any missed points. Thank you for reading.