'Spring-Boot: How to create Rest API service without using beans?

Spring-boot is the industry standard for creating REST API service in Java. You can see here a code example from the official documentation of sprint-boot: https://spring.io/guides/gs/spring-boot/

Whereas spring-boot is a great solution for creating REST API, it seems from the documentation that I must use the Spring framework for dependency injection. I want to use spring-boot, but I don't want to use any dependency injection framework. My intention is just to run a "public static void main" method and do all the injection inside this method.

Do you know how to do it? Or should I pick another framework?



Solution 1:[1]

The problem is within the roots of your question.

You are saying "I want to use Spring Boot, but I don't want to use Spring Framework." Seems like you don't completely understand what is Spring Boot and what is Spring Framework.

From official Spring page:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

What it means is that Spring Boot is just a set of settings, or you can say preconfigurations FOR Spring Framework generally.

But what is Spring Framework?

The core concept of Spring Framework is so called IoC Container (Inversion of Control). Or simply saying, IT IS ALL ABOUT DEPENDENCY INJECTION. There is a thing called Application context, which is managed by Spring Framework.


Now another question. Why would you not use Spring Framework? What's exactly something you don't like with it? Dependency Injection? You want to always create a new object? Why?

Again, in the core, by default, Spring Framework creates Beans using Singleton pattern, which basically beans that, one instance of a class is getting created, and then it is stored inside Application Context by Spring Framework. When you will use it, Spring Framework IoC Container will just fetch it for you, not create it again. And this is absolutely great, since you are saving a lot of memory.

Spring Framework also lets you change that behaviour, so you can select Bean to be not Singleton, but Prototype, which basically means, each time you call an object is getting created. Each call, each new object.

And this is cool that Spring Framework actually gives you this choice, even though you don't really need it 99/100 time.


If you are sure that Spring is something you don't want to use, if you want to go deeper, then there is an entire Java EE (today also called Jakarta EE) which you can use, which will give a lot more freedom.

Frameworks won't give you much freedom, that's the purpose of frameworks. "Set of rules, premanaged for you, to make your life easier."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1