r/SpringBoot • u/Jason_hill100 • Oct 20 '22
what beans exactly are
Hi there, I recently started web dev with spring, and i don't quite get what beans are and what's there utility
Like i know that they are java object, but that's it And why we need a bean factory since we are creating the classes ourselves
17
u/reddit04029 Oct 20 '22
In simple terms, it’s a java object handled by the IoC container.
If the IoC container knows about this object, you can inject it as a dependency by autowiring it.
4
4
u/Davekave9 Oct 20 '22
Beans are objects whose instantiation is managed by the application context. Think about inversion of control: you don't instantiate beans by calling their new() method (and in consequence of that, you don't need to know what parameters they need to get instantiated), but by telling the application context to inject an instance of them via the @Autowired annotation.
5
Oct 20 '22 edited Oct 20 '22
You asked this in a spring forum so you are getting a lot of spring based answers, but it’s actually simpler than IoC. Java Beans are more or less just a formulaic way of making an object that allows the framework or JVM to attach additional functionality without you having to explicitly write them (like getters and setters.)
Here is a stack overflow question and answer the covers both spring and Java beans. https://stackoverflow.com/questions/21866571/difference-between-javabean-and-spring-bean
What it comes down to in both cases is that you get additional functionality out of a class due to following conventions.
1
u/diveIntoDevelopment Oct 20 '22
It helps to scope your beans (objects) without having boilerplate code.
0
u/davidewan_ Oct 20 '22
Aren't they singletons? I have a long C++ history but I was working in spring a while ago and thats what I settled on.
1
u/pronuntiator Oct 20 '22
Not necessarily. For example, a request scoped bean instance will exist per request.
30
u/HistoricalSchedule5 Oct 22 '22 •
Hi, I wanted to provide a pretty detailed response because I struggled a lot with Spring at the beginning, and it only clicked after a few months.
Beans are indeed java objects. What's specific about them is that they are managed by Spring IoC container. IoC means "Inversion of Control". That is, if you have an object which relies on other objects to function properly, you let Spring handle the relationships (dependencies) between these objects instead of doing it yourself.
For example, say you have a project where you need to expose an HTTP endpoint through a Controller object. Since it's very important to only affect a single responsibility to an object, your controller is only responsible for handling HTTP requests. Then you have your Service object which is responsible for handling the business logic, and your Repository object which is responsible for performing operations on the database. And then your repository object needs to have access to the database schema so you need to give it the address and port of your database. Since you most likely have different databases for you test environment and your production environment, you need to handle this case as well.
If you try to manipulate these objects in an intuitive way, it could give something like this (let's say your project is about managing stock for a retail company) . This is extremely simplified, and there are way more dependencies between these objects but it's a simple example.
Let's go bottom-up, you first need to create your repository
}
Then you need to create your service.
}
Then you can create your Controller
Then say you have to actually create your controller somewhere.
You would need something like :
This is very repetitive and you would most likely need to write the exact same code for the production environment.
Now, without inversion of control you would have to handle dependencies between your objects yourself all the time. Sure you can find ways to optimize it, I didn't bother and wrote it the worst possible way, but still, it is a lot of boilerplate and repetition.
With inversion of control, Spring basically scans your project for classes and creates instances of objects (beans) for every class that is annotated with bean-related annotations (such as "@Configuration", "@Repository", "@Service" "@Controller" etc..). All of these objects are stored in the Spring Context, which you can see basically as an array of beans.
This allows you to simply declare relationships between beans and Spring will go find in its context if a corresponding bean is available. If it is, it will inject it in your object without you needing to manually create an instance and pass it through to the constructor.
For example :
}
This looks very similar to the non-IoC way however there's a very important nuance here. You will never instantiate your controller yourself. You can just write this code and launch your spring boot application and when constructing an instance of your controller class, Spring will detect that it needs a service, which needs a repository which needs a database configuration and it will look for all these beans in the Spring Context.
Furthermore, you can configure the Spring Context to your liking, using things such as the "@Profile" annotation. Let's go back to the issue with the development database and the production database. Instead of constantly passing around environment values to objects which simply do not use it and do not care about it, you can simply declare two different beans of the same type and tell Spring that they are only to be used if a specific profile is active :
Then, according to the profile you wish to use when launching your application (you can specify it with your IDE or in the command line), Spring will only add the bean annotated with the correct profile to its context.