What Is Spring Bean - JavaTechOnline
What Is Spring Bean - JavaTechOnline
Search Content...
Super Selectos
ABRIR
Ofertas todos los días
You are here Home > java >
Before discussing ‘What is Spring Bean?’, let’s know ‘What is Bean Class or a Component Class?’
Table of Contents
1. What is Bean Class/Component Class?
2. What is the difference between Java Bean & Bean Class?
3. What is Spring Bean?
4. How to mark a class as a Spring Bean?
4.1. Using XML Configuration File
4.2. Using Annotation
4.3. Using Java-based Configuration
5. How to configure inheritance in Spring Bean?
5.1. Using @Bean annotation TOP
5.2. Using XML configuration file
6. FAQ
6.1. Q1. Can We take a component class as a Spring Bean?
6.2. Q2. Can every component class be a Spring Bean?
6.3. Q3. Is every Java Bean class a Spring Bean?
6.4. Q4. Can an abstract class be a Spring Bean?
6.5. Q5. Can a user defined non-POJO class be a Spring Bean?
6.6. Q6. Does every POJO class implicitly become a Spring Bean?
6.7. Q7. What all Annotations can we use with a Spring Bean?
6.8. Q8. What is the difference between @Bean and @Component?
Bean and Component are the similar terms which are used interchangeably most of the times. Bean class is the Sun
MicroSystem’s terminology, whereas Component is the Microsoft’s terminology.
In order to let Spring Container recognize a class as a Spring Bean, we need to mark that class either in the XML configuration
file or by using annotations.
TOP
Using XML Configuration File
Use <bean> tag in the configuration file to mark a Java class as a Spring Bean. This configuration file provides instructions to
the Spring Container to consider the class as a Spring Bean. Furthermore, Spring Container handles the whole life cycle of that
Spring Bean such as loading of the spring bean class, creating objects of the spring bean class, managing object of the spring
bean class, and destroying the spring bean class. For example, below code represents that the Employee class is a Spring
Bean.
Here, id becomes the object name for the Spring Bean internally. The class attribute must take the fully qualified class name.
Using Annotation
One of the other and the most popular way to mark a Java class as a Spring Bean is by using Annotations. When we apply
@Component annotation in a Java class, it is considered as a Spring Bean by the Spring Container. However, there are many
other annotations that can do the same job, but the @Component is the basic and the most widely used by the developers.
For example, below Employee class is a Spring Bean.
@Component
public class Employee{
. . . .
}
If we don’t specify any name, the class name with first letter in lower case will become the name of the Spring bean by default.
In this case it will be ’employee’. However, we can provide the name of Spring Bean as @Component(“employee”).
We also have various Stereotype annotations that we can use as an alternative to @Component, but we should use them at
some specific scenario. @Component is a class level annotation. Other stereotypes are a specialization of @Component.
During the component scanning, Spring container automatically discovers the classes annotated with @Component, It
registers them into the Application Context as a Spring Bean.
Ads by
Stop seeing this ad
Since Spring 3.0, a pure-Java configuration container was provided. XML configuration is completely removed in this
approach. The key features in Spring Framework’s new Java-configuration support are @Configuration annotated classes and
@Bean annotated methods. Using @Configuration annotation at any Java class represents that Spring container will consider
TOP
it as a source of Bean’s definitions. Using the @Bean tells the Spring container that method will return an object which should
be registered as a Spring bean in the Spring container.
Ads by
Stop seeing this ad
Replay
@Configuration
public class EmployeeConfig{
@Bean
public Employee getEmployee(){
return new Employee();
}
}
As of now, we should have a clear understanding of ‘What is Spring Bean?’. Let’s have a basic understanding of ‘How
inheritance work in Spring Bean?’.
1) Define a parent bean configuration using the @Bean annotation in a configuration class as shown below:
@Configuration
public class ParentConfig {
@Bean
public ParentBean parentBean() {
// return the bean instance
}
}
TOP
2) Inherit from the parent bean configuration by defining another configuration class and using the @Bean annotation with
the parent attribute.
@Configuration
public class ChildConfig extends ParentConfig {
@Bean(parent = "parentBean")
public ChildBean childBean() {
// return the bean instance
}
}
In the above example, ChildBean inherits from ParentBean, which is defined in the ParentConfig class.
Ads by
Stop seeing this ad
<beans>
<bean id="parentBean" class="com.example.ParentBean"/>
</beans>
2) Inherit from the parent bean configuration by defining the child bean configuration and using the parent attribute.
<beans>
<bean id="childBean" class="com.example.ChildBean" parent="parentBean"/>
</beans>
In the above example, ChildBean inherits from ParentBean, which is defined in the XML file.
When a bean is defined as a child bean, it inherits all the properties of the parent bean, including its dependencies,
initialization, and destruction methods. However, we can also override or extend the properties of the parent bean in the child
bean, if necessary.
Hope that this is enough to know ‘What is Spring Bean?’, and its related concepts.
FAQ
TOP
Let’s talk about some FAQs to make your concept crystal clear on ‘What is Spring Bean?’. It may help you in the interviews
also.
TOP
Q8. What is the difference between @Bean and @Component?
In Spring Framework, both @Bean and @Component annotations are used for creating beans. However, there are some
technical differences between them:
1) We can apply @Bean to methods of a @Configuration annotated class that creates a bean, whereas @Component is used
to annotate classes.
2) @Bean provides a lot of flexibility in configuring the bean, such as setting the scope, setting up dependencies, and
initializing the bean, whereas @Component is a more general-purpose annotation and does not provide as much
configuration flexibility.
Me gusta
Tagged bean class in java bean in spring Creating and Configuring Spring Beans difference between @bean and @component java bean class
java spring bean Java Spring Beans spring bean Spring Bean Definition spring bean java spring beans what is a spring bean what is bean
what is bean in spring What is Spring Bean
Previous article
New Features In Spring Framework 6
Next article
Spring Scheduling Cron Expression
Leave a Reply
Paragraph
sf-segoe-ui 16px
Formats
Name *
Email Address *
TOP