You are on page 1of 7

JavaTechOnline

Making Java Easy To Learn

Search Content...

Super Selectos
ABRIR
Ofertas todos los días
You are here Home > java >

What Is Spring Bean


java Spring Spring Boot by devs5003 - January 10, 2023  0

Every Java Developer come across the terms Java


Bean, Bean Class & Spring Bean. Most of the times
people think that all are the same and used
interchangeably. Even sometimes people consider
them as a simple POJO class. In fact, these all have
some logical differences. There are some set of
standards & guidelines provided by Programmers
that make them different from each other. In this
article ‘What is Spring Bean?’, we will start with the
fundamentals which are also essential to know, then
we will discuss the Spring Bean with examples.

Please note that if you define a class that doesn’t


follow the guidelines of being a Spring Bean, the Java
compiler will never complain you. In contrast, the
Spring Container will complain you that it is unable to
find any Spring Bean at runtime. Hence, it is the programmer’s responsibility to mark a class as a Spring Bean so that the
Spring Container can recognize it manage its life cycle accordingly. Let’s discuss our article ‘What is Spring Bean?’, and its
related concepts.

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?

What is Bean Class/Component Class?


A bean is a Java class that contains state and behavior and state must be used in the business logic of the behavior.
Programmatically, the non-static member variables represent the state of the class. On the other hand, methods represent the
behavior of the class. It is always recommended to declare member variables as private and methods as public so that
member variables can be accessed only through methods, but not directly. For example, Service classes, DAO classes etc. are
considered as Bean classes or Component classes.

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.

What is the difference between Java Bean & Bean Class?


The difference between Java Bean & Bean class is that a Bean class contains business logic methods, whereas a Java bean is a
simple class with state, behavior, getters, setters and it doesn’t contain business logic methods. This is the logical difference
only and set of guidelines that a programmer should follow. However, the compiler will never complain that you are not
following the guidelines in such cases as these guidelines/standards are not defined by the Java compiler.

What is Spring Bean?


A Java class whose object is created and managed by the Spring Container is called a Spring Bean. Moreover, we can consider
any class as a Spring Bean, except an abstract class and an interface. It can also be a POJO class, Java Bean class or any other
class as well.

How to mark a class as a Spring Bean?


After knowing ‘What is Spring Bean?’, let’s discuss how to mark a Java class as a spring Bean.

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.

<bean id="employee" class="com.dev.example.Employee"/>

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.

Using Java-based Configuration


There is one more way to mark a Java class as a Spring bean.

Ads by
Stop seeing this ad

Why 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

Why this ad? 

Replay

@Bean annotation plays the same role as the <bean/> element.

For example, below code demonstrates the concept of Java-based configuration:

@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?’.

How to configure inheritance in Spring Bean?


Generally, there are two approaches to configure inheritance in Spring Bean.

1) By using the ‘parent’ attribute in the @Bean annotation

2) In the XML configuration file by using ‘parent’ attribute.

Using @Bean annotation


Here’s how we can do it:

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

Why this ad? 

Using XML configuration file


1) Define a parent bean configuration in the XML file as shown below:

<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.

Q1. Can We take a component class as a Spring Bean?


Yes. Any class can be taken as a Spring Bean, except abstract class and interface.

Q2. Can every component class be a Spring Bean?


No, unless we mark it as a Spring Bean.

Q3. Is every Java Bean class a Spring Bean?


No, unless we mark it as a Spring Bean.

Q4. Can an abstract class be a Spring Bean?


No. Abstract class and interface can’t be a Spring Bean.

Q5. Can a user defined non-POJO class be a Spring Bean?


Yes. Any Java class, except interface & abstract class can be a Spring Bean.

Q6. Does every POJO class implicitly become a Spring Bean?


No, unless we mark it as a Spring Bean.

Q7. What all Annotations can we use with a Spring Bean?


There is a large list of annotations that we can use with a Spring Bean. It’s not possible to explain each of them here. For that,
kindly visit a separate article on ‘Spring Bean Annotations With Examples‘.


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

File Edit View Insert Format Tools Table

Paragraph            
sf-segoe-ui 16px         
       Formats  

  
Name *

Email Address *

TOP

You might also like