I am yet to find a high-level definition of Spring beans that I can understand. I see them referenced often in Grails documentation and books, but I think that understanding what they are would be beneficial. So what are Spring beans? How can they be used? Do they have something to do with Dependency Injection?
The Spring core technologies reference documentation describes what beans are.
Per the Introduction to the Spring IoC Container and Beans section (where "IoC" means "inversion of control"):
In Spring, 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 managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
Beans and scope are described in the Bean Scopes section:
When you create a bean definition, you create a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, as with a class, you can create many object instances from a single recipe. You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition but also control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because you can choose the scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes.
Spring beans are just instance objects that are managed by the Spring container, namely, they are created and wired by the framework and put into a "bag of objects" (the container) from where you can get them later.
The "wiring" part there is what dependency injection is all about, what it means is that you can just say "I will need this thing" and the framework will follow some rules to get you the proper instance.
For someone who isn't used to Spring, I think Wikipedia Spring's article has a nice description:
Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together. Objects created by the container are also called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions which provide the information required to create the beans. Objects can be obtained by means of either dependency lookup or dependency injection. Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods.
First let us understand Spring:
Spring is a lightweight and flexible framework.
https://i.stack.imgur.com/UXWKb.jpg
Java Beans are classes that encapsulate many objects into a single object (the bean). The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.
Spring Bean: is an object, which is created, managed and destroyed in Spring Container. We can inject an object into the Spring Container through the metadata(either xml or annotation), which is called inversion of control.
Analogy: Let us assume farmer is having a farmland cultivating by seeds(or beans). Here, Farmer is Spring Framework, Farmland land is Spring Container, Beans are Spring Beans, Cultivating is Spring Processors.
https://i.stack.imgur.com/UugO2.jpg
Like bean life-cycle, spring beans too having it's own life-cycle.
https://i.stack.imgur.com/kpxyA.jpg
https://i.stack.imgur.com/TDLCr.png
Following is sequence of a bean lifecycle in Spring:
Instantiate: First the spring container finds the bean’s definition from the XML file and instantiates the bean.
Populate properties: Using the dependency injection, spring populates all of the properties as specified in the bean definition.
Set Bean Name: If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.
Set Bean factory: If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method.
Pre-Initialization: Also called post process of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.
Initialize beans: If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called.
Post-Initialization: – If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
Ready to use: Now the bean is ready to use by the application
Destroy: If the bean implements DisposableBean, it will call the destroy() method
Spring is a lightweight [...] framework.
is just absolutely ridiculous.
Well you understood it partially. You have to tailor the beans according to your need and inform Spring container to manage it when required, by using a methodology populalrly known as IoC (Inversion of Control) coined by Martin Fowler, also known as Dependency Injection (DI).
You wire the beans in a way, so that you do not have to take care of the instantiating or evaluate any dependency on the bean. This is popularly known as Hollywood Principle.
Google is the best tool to explore more on this in addition to the links you would get flooded with here in this question. :)
A Bean is a POJO(Plain Old Java Object), which is managed by the spring container.
Spring containers create only one instance of the bean by default. This bean it is cached in memory so all requests for the bean will return a shared reference to the same bean.
The @Bean annotation returns an object that spring registers as a bean in application context. The logic inside the method is responsible for creating the instance.
When do we use @Bean annotation?
When automatic configuration is not an option. For example when we want to wire components from a third party library, because the source code is not available so we cannot annotate the classes with @Component.
A Real time scenario could be that someone wants to connect to Amazon S3 bucket. Because the source is not available he would have to create a @bean.
@Bean
public AmazonS3 awsS3Client() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsKeyId, accessKey);
return AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
}
Source for the code above -> https://www.devglan.com/spring-mvc/aws-s3-java
Because I mentioned @Component Annotation above.
@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and class path scanning.
Component annotation registers the class as a single bean.
Spring have the IoC container which carry the Bag of Bean ; creation maintain and deletion are the responsibilities of Spring Container. We can put the bean in to Spring by Wiring and Auto Wiring. Wiring mean we manually configure it into the XML file and "Auto Wiring" mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.
Here is the detail URI where you got more information about Beans
Spring beans are just object instances that are managed by the Spring IOC container.
Spring IOC container carry the Bag of Bean.Bean creation,maintain and deletion are the responsibilities of Spring Container.
We can put the bean in to Spring by Wiring and Auto Wiring.
Wiring mean we manually configure it into the XML file.
Auto Wiring mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.
Spring beans are classes. Instead of instantiating a class (using new
), you get an instance as a bean
cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.
In terms of a Spring boot application, a bean is simply a Java object which is created by Spring framework when the application starts.
The purpose of the object can be pretty much anything - a configuration, a service, database connection factory etc. - Spring doesn't really care.
Most beans depend on other beans to work, for example an entity manager might need a database connection. Spring framework is able to figure out how the beans should be wired together automatically. From your point of an application developer, you just have to declare the beans you need and they "magically" appear in your application ready to use.
The XML configuration of Spring is composed of Beans and Beans are basically classes. They're just POJOs that we use inside of our ApplicationContext. Defining Beans can be thought of as replacing the keyword new. So wherever you are using the keyword new in your application something like:
MyRepository myRepository =new MyRepository ();
Where you're using that keyword new that's somewhere you can look at removing that configuration and placing it into an XML file. So we will code like this:
<bean name="myRepository "
class="com.demo.repository.MyRepository " />
Now we can simply use Setter Injection/ Constructor Injection. I'm using Setter Injection.
public class MyServiceImpl implements MyService {
private MyRepository myRepository;
public void setMyRepository(MyRepository myRepository)
{
this.myRepository = myRepository ;
}
public List<Customer> findAll() {
return myRepository.findAll();
}
}
In Spring, those objects that form the backbone of your application and that are managed by the Spring IoC container are referred to as beans. A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC container;
For Spring, all objects are beans! The fundamental step in the Spring Framework is to define your objects as beans. Beans are nothing but object instances that would be created by the spring framework by looking at their class definitions. These definitions basically form the configuration metadata. The framework then creates a plan for which objects need to be instantiated, which dependencies need to be set and injected, the scope of the newly created instance, etc., based on this configuration metadata. The metadata can be supplied in a simple XML file, just like in the first chapter. Alternatively, one could provide the metadata as Annotation or Java Configuration.
Book: Just Spring
Success story sharing
@Autowired
? Or simply when I do myimport
s?