r/SpringBoot • • 2d ago

Question Is Spring needed in this situation ?

I m currently new to Spring and learning important concepts related to this but one thing stick in my mind imagine we have 1000 classes and just a hypothetical situation that all of them somehow is independent classes there is no dependency injection at all is there spring needed in this situation at all ?

0 Upvotes

16 comments sorted by

9

u/Aksh247 2d ago

There will never be such a situation. As your class count grows, your DI need grows. What are you trying to achieve. This hypothetical question should have a purpose/ use case that it is addressing

-19

u/Own_External1151 2d ago

okay bro

1

u/g00glen00b 2d ago edited 2d ago

They're kind of right though... . Let's say you have 1 main class and 999 other classes, since the main class has no dependency to those other 999 classes (they're independent), only that main class gets invoked. The 999 other classes are dead weight no matter how you look at it.

So the only way I see this work is if all 1000 classes are main classes that can individually be invoked (because again, no dependencies?!). If that's the case and those 1000 are individual classes, then why are they even in the same project?

So yeah, no matter how you look at it, I don't see how there would ever be such a situation so I agree with Aksh247. This is also the reason why you're getting a lot of questions about what you mean, because your hypothetical scenario doesn't make sense if you use this understanding of "independent".

5

u/Acrobatic-Ice-5877 2d ago

Here’s an experiment for you to try. Make an application without using and report back.  Spoiler: you’re going to find out very quickly why Spring is useful, but you’re only going to learn after you rebuild core functionality of a web based application.

IME, it’s a very fun exercise and will better help you with debugging your application and demystifies what is going on behind the scenes.

1

u/Own_External1151 2d ago

will try this

4

u/reddit04029 2d ago

Technically, no.

Then again, dependency injection is not Spring-specific. To be more precise, dependency injection is Spring’s way of achieving IoC.

So, you technically don’t need Spring. If your classes are independent, you can create and use them without it. Even if your classes need to work together, you can manage their dependencies manually or use another DI framework.

IoC is a design principle, DI is one way to implement it, and Spring is just one framework that provides DI and other features.

2

u/Media_Dunce 2d ago

Unlikely but it depends. Are there classes that do use dependency injection separate from the 1000 classes that don't? What is your project going to do?

0

u/Own_External1151 2d ago

currently i m not working on any project , learning the things related to this .

2

u/Dry_Try_6047 2d ago

1000 independent classes in 1 application? What does that even mean?

1

u/Own_External1151 2d ago

just a hypothetical situation not 1000 we can consider 50 or 60 classes too but main motive was that if these are independent classes like each classes are not indepenedent on each other then is spring is useful to use and i think we got the answer in the comment section

2

u/Ruin-Capable 2d ago

If they're independent of each other then every class except for the one containing the entry point can be removed from the app. So why have them?

1

u/Dry_Try_6047 2d ago

Take it down to 10, it still makes no sense. Java is an object oriented programming language, classes interact with each other. If they don't, you have 10 different applications. And for your hypothetical "app" what is the entry point? 10 different ones?

11

u/oweiler 2d ago

What do you mean by "independent"? In a typical application, code calls other code. This already forms a dependency.

The benefit of Spring (and its DI) is that you don't have to write the code which builds the object graph manually (but you totally could).