Spring Beans
Backbone of Spring Application
Today I have landed into the Spring Planet.
Hence learning about century old high-tech that will help on building the back-end of the Command Deck for Microgreens and Hydroponics.
Spring has some cool things. Starting with Beans.
What is a Spring Bean?
Apart from being a superfood, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
Each "Bean" is kind of a unique seed
that we are able to plant inside the Spring Container
or Context for the Back-end of the application.
Every Bean is an Object with the @Component attribute to it.
Sad story? The Bean has always a predefined future.
Be cleaned up by the Garbage Collector.
How to declare Beans
We can create Beans in our Universe by adding them to our AppConfig:
@Configuration
public class AppConfig {
@Bean
public AwesomeBean awesomeBean() {
return new AwesomeBeanImpl();
}
}How to get Beans
Since we are able to get our hands dirty into the soil (Spring Container Context), we can use the getBeans() method for accessing its inner powers:
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...);
AwesomeBean awesomeBean = context.getBean(AwesomeBean.class);
awesomeBean.usePower(); // BAM!Instead of relying on pre-defined Beans from some frameworks,
being able to create custom Beans is like being able to create new Microgreens seeds based on your own criteria. Isn't that awesome?
End log.