Spring Bean Interview Questions and Answers

More Java interview questions and answers...

1. What is Spring bean?

Spring beans are objects which are engendered and managed completely by spring container. Beans can be defined in spring either by utilizing XML configuration or by utilizing Annotation.
  • In XML configuration, bean can be defined utilizing < bean > tag inside < beans > tag.
  • In the Annotation configuration, the bean can be defined utilizing the annotations like @Component, @Service, @Controller, @Repository on top of the class definition.
  • We can withal achieve beans definition thoroughly in java class by utilizing java configuration.

2. How many bean scopes are supported by Spring?

The Spring Framework fortifies five scopes. They are:
  • Singleton: This provides scope for the bean definition to a single instance per Spring IoC container.
  • Prototype: This provides scope for a single bean definition to have any number of object instances.
The below three are available only if the users utilize a web-aware ApplicationContext.
  • Request: This provides scope for a bean definition to an HTTP request. 
  • Session: This provides scope for a bean definition to an HTTP session. 
  • Global-session: This provides scope for a bean definition to a global HTTP session.

3. What is the default bean scope in Spring? 

Singleton is the default scope for a Bean, the one that will be used if nothing else is designated.

4. Explain the bean cycle in the container?

A bean is created, utilized, and finally destroyed. It is an object in an application. The entire spring bean life cycle is supervised by the Spring IoC (Inversion of Control) container.
  • Instantiation: Spring instantiates bean objects just like we would manually create a Java object instance.
  • Populating Properties: Using the dependency injection, spring populates all of the properties as specified in the bean definition.
  • Set Bean Name: If the bean implements the BeanNameAware interface, the spring passes the bean’s id to the setBeanName() method.
  • Set Bean factory: If Bean implements the BeanFactoryAware interface, the spring passes the bean factory to the setBeanFactory() method.
  • Pre-Initialization: Spring’s BeanPostProcessors get into action in this phase. The postProcessBeforeInitialization() methods do their job. Withal, @PostConstruct annotated methods run right after them.
  • 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.
  • Custom Initialization: Spring triggers the initialization methods that we defined in the init method attribute of our @Bean annotation.
  • Post-Initialization: If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
  • Ready to utilize: Now the bean is ready to utilize by the application
  • Destroy: If the bean implements DisposableBean, it will call the destroy() method

5. What is bean wiring?

Bean wiring is the process of combining beans with a Spring container. The required beans are to be apprised to the container and how the container should utilize dependency injection to tie them together, at the time of wiring the beans.

6. What does the @Bean annotation do?

@Bean annotation denotes that the annotated method engenders a bean to be managed by the Spring container. It is a direct analog of the <bean/> XML tag. @Bean fortifies most of the attributes offered by <bean/>, such as init-method, destroy-method, auto wiring, lazy-init, dependency-check, depends-on, scope.

7. What are inner beans in Spring?

Inner beans are spring beans that are utilized as a property of another spring bean. Spring's XML-based configuration metadata provides the utilization of <bean/> element inside the <property/> or <constructor-arg/> elements of a bean definition to declare inner beans. Inner beans are always incognito and it is always scoped as archetypes.

8. Are Singleton Beans Thread-Safe?

No, singleton beans are not thread-safe, as thread safety is about execution, whereas singleton is a design pattern fixating on engendered. Thread safety depends only on the bean implementation itself.

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024