Beans
Learn about a few interview questions regarding beans.
We'll cover the following
- What are beans in Spring?
- What is the lifecycle of a Spring Bean?
- What are custom bean lifecycle methods?
- What are some features of custom init and destroy methods?
- What information does the bean definition contain?
- How can you provide a bean id when using annotations?
- Are Spring beans the same as JavaBeans?
- How are beans created?
- What does the @Bean annotation do?
- Both @Bean and @Component annotations create beans. What is the difference between the two?
- How can dependencies be injected using the @Bean annotation?
- What are the different scopes of a bean?
- What is the default bean scope in Spring?
- What is the default scope in the web context?
- When are singleton and prototype scopes used?
- How is bean scope defined?
- Is the Singleton scope in Spring same as the Singleton design pattern?
- Are Singleton beans thread safe?
- Explain prototype bean scope.
- Does Spring manage the complete lifecycle of beans?
- What is an inner bean?
What are beans in Spring?
Simply put, a Spring bean is a Java object. When Java objects are created by the Spring container, they are referred to as Spring Beans.
Beans are managed by the Spring container using the configuration metadata in the form of XML or Java annotations. The container instantiates, assembles and manages the lifecycle of a bean. For example, the @Component
annotation on a class tells Spring framework that it has to manage the lifecycle of the objects of that class.
What is the lifecycle of a Spring Bean?
Spring container instantiates a bean and initializes it. It also injects the required dependencies. When the context is destroyed, all the initialized beans are also destroyed.
Spring provides post initialization and pre destruction methods for custom tasks. These methods can be invoked using XML config file or Java annotations.
What are custom bean lifecycle methods?
During the lifecycle of the bean, Spring allows the developer to add custom code during bean initialization and bean destruction. This can include code for custom business logic at initialization or destruction as well as setting up and cleaning up resources like a database connection or a file etc.
The XML tags init-method
and destroy-method
are used to define the custom methods inside the <bean>
tag. The @PostContruct
and @PreDestroy
annotations also accomplish the same task.
What are some features of custom init and destroy methods?
- Custom methods for initialization and destruction can have any name.
- They can have any access modifier; private, public, or protected.
- They can have any return type, but since the return value cannot be captured, void is mostly used as the return type.
- They cannot have any input arguments.
What information does the bean definition contain?
Bean definition contains information for the container in the form of configuration metadata. Bean’s definition contains the following information:
- How the bean is created.
- Lifecycle details of the bean.
- Dependencies of the bean.
How can you provide a bean id when using annotations?
We can specify the bean id in the component scan annotation.
@Component("bean1")
public class MyBean {
...
}
When Spring registers MyBean
class as a bean, it will store bean1
as the bean id. The following code retrieves the bean:
MyBean theBean = context.getBean("bean1", MyBean.class);
If explicit bean id is not provided Spring generates a default bean id. The default bean id is the class name with the first letter lower-case.
@Component
public class MyBean {
...
}
Now, when Spring registers MyBean
class as a bean, it will store myBean
as the bean id. To retrieve the bean from the container, we need to use the default bean id:
MyBean theBean = context.getBean("myBean", MyBean.class);
Are Spring beans the same as JavaBeans?
JavaBeans are Java classes that follow certain coding conventions. They have a public
no-arg constructor, have private
properties allow access to properties using getter and setter methods and implement the java.io.Serializable
interface.
Spring beans are objects whose lifecycle is managed by the Spring container. They do not follow the rigorous requirements of JavaBeans.
Spring beans are often the same as JavaBeans but they don’t have to be. Spring beans can have a constructor with arguments and may not implement the java.io.Serializable
interface.
How are beans created?
Spring framework creates beans in an order. When Spring framework encounters a bean definition in XML file or through an annotation, it checks if the class is dependent on any other class. Suppose class A has a dependency on class B. In this case, Spring will create the object of class B. Once the dependency has been created, the bean for class A can be created by injecting the bean of class B in class A.
What does the @Bean
annotation do?
The @Bean
annotation is used on a method to indicate that the method returns a bean to be managed by Spring. The name of the method indicates the bean id. This annotation is used in classes marked with the @Configuration
annotation.
The @Bean
annotation provides the same functionality as the <bean>
tag in XML configuration.
The following configuration class creates the a bean of Vehicle
type using the @Bean
annotation. The bean id is vehicle
.
Get hands-on with 1400+ tech skills courses.