Top Spring Beans Interview Questions (2021) | CodeUsingJava
















Most frequently asked Spring Beans Interview Questions


  1. Explain Spring Beans?
  2. How can you provide configuration metadata to the Spring container?
  3. How many bean scopes does Spring support?
  4. Explain the difference between JavaBean and Spring bean?
  5. Define bean life cycle in Java Spring?
  6. Explain the difference between @Component and @Beans annotations in Spring?
  7. Explain bean wiring?
  8. Explain auto wiring and its types?
  9. What are the limitations of auto wiring?
  10. What do you mean by @RequestMapping annotation?


Explain Spring Beans?

A bean is an object that is instantiated, constructed, and managed by a Spring IoC container. The configuration metadata that you supply to the container is used to create these beans. They are the objects that create the user's application's backbone.


How can you provide configuration metadata to the Spring container?

The three most essential techniques for providing configuration metadata to the Spring Container are listed below.
  1. XML based configuration Dependencies and services in Spring Framework that are required by beans are specified in configuration files which are in XML format. Bean definitions and application-specific configuration options are mainly found in these configuration files. They begin with a bean tag.
  2. Annotation-based configuration Annotations on the relevant class, method, or field declaration can be used to configure the bean into the component class itself instead of using XML to describe bean wiring. In the Spring container, annotation wiring is disabled by default. Before using it, you need to enable it in your Spring configuration file.
  3. Java-based configuration @Configuration annotated classes and @Bean annotated methods are two key features of Spring Framework's new Java configuration support.
    1. The @Bean annotation is similar to the element.
    2.Inter-bean dependencies can be defined using @Configuration classes by simply calling other @Bean methods in the same class.


How many bean scopes does Spring support?

The Spring Framework supports the five scopes mentioned below.
  • singleton(default) When you set the scope as singleton, only one instance of the object is created by the Spring IoC container defined by that bean definition.Singleton is always the default scope.
      <bean id = "..." class = "..." scope = "singleton">
       </bean>
    
  • Prototype scope when scope is set to prototype, the new bean instance of the object is created by Spring IoC container whenever a request for that specific bean is made.
      <bean id = "..." class = "..." scope = "prototype">
        </bean>
    
  • request. It scopes a single bean definition to the lifetime of a single HTTP request, Only in the context of a web-aware Spring ApplicationContext is this valid.
  • session It scopes a single bean definition to the lifecycle of an HTTP Session.
  • global-session This sets the scope of a bean definition to the entire HTTP session. Only in the context of a web-aware Spring ApplicationContext is this valid.


Explain the difference between JavaBean and Spring bean?

  • JavaBean JavaBeans are, at their most basic level are the java classes that follow particular coding principles. Particularly, classes that have public default (no argument) constructors,that also implement java.io.Serializable and by using accessor (getter and setter) methods, they can give access to their properties.
  • Spring bean A Spring bean is an object that Spring manages. Spring beans are defined in Spring configuration files and then instantiated by Spring containers before being injected into applications. Spring Bean does not need to be serializable. Spring Bean does not need to have a default no-arg constructor.


Explain bean life cycle in Java Spring?

The spring container controls the bean life cycle. When we run the program, the spring container is first started. After that, the container builds a bean instance based on the request and then injects dependencies. Finally, when the spring container is closed, the bean is destroyed. As a result, if we want to run any code on bean instantiation and immediately after closing the spring container, then we can write that code inside custom init() and destroy() methods.Instead of init() and destroy(), we can use a custom method name.



Explain the difference between @Component and @Beans annotations in Spring?

  • @Component
  • @Component uses classpath scanning to automatically detect and configure the beans.
  • The declaration of the bean is not decoupled from the class definition in @Component
  • If the class is outside the spring container, we cannot build a bean of that class using @Component
  • @Controller, @Repository, and @Service are specialisations of @Component
  • @Component is a class-level annotation
  • Bean
  • @Bean declares a single bean explicitly rather than letting on Spring to do it automatically.
  • The declaration of the bean is decoupled from the class definition in @Bean.
  • If the class is outside the spring container,we can create a bean of that class using @Bean.
  • @Bean has no specialisations
  • @Bean is a method-level annotation


Explain bean wiring?

The process of combining beans with a Spring container is known as bean wiring. At the time of wiring the beans, the required beans must be informed to the container, as well as how the container should use dependency injection to connect them.

Explain auto wiring and its types?

In Spring, auto wiring is the process of looking for objects that have the same name as an object property. The auto wiring feature is enabled by default in the Spring framework. There are four types of autowiring modes
  1. no This is the default mode, which means there will be no auto wiring and you will need to use explicit bean references to wiring. You don't need to do anything special for this wiring.
  2. byName Autowiring by property datatype. Spring container examines the characteristics of beans for which the auto-wire property is set to byName in the XML configuration file. It then tries to match and wire its attributes to the beans with the same names given in the configuration file.
  3. byType Autowiring by property name. The properties of the beans on which the auto wire attribute is set to byType in the XML configuration file are examined by the Spring container. It then attempts to match and wire a property if its type corresponds to one of the beans listed in the configuration file. If there are more than such beans, a fatal exception is raised.
  4. constructor This is similar to byType, except that it applies to constructor arguments. A fatal error is raised if the container does not contain exactly one bean of the constructor argument type.
  5. autodetect Spring tries to wire using autowire by constructor first, then it tries to autowire by byType if that doesn't work.


What are the limitations of auto wiring?

The following are some of the limitations you may face when it comes to auto wiring:
  • Overriding possibility Dependencies can be specified using <constructor-arg> and <property> settings which will always override autowiring.
  • Confusing nature Because autowiring is less precise than explicit wiring, use explicit wiring whenever possible.
  • Primitive data types Simple attributes like primitives, Strings, and Classes cannot be autowired.


What do you mean by @RequestMapping annotation?

The @RequestMapping annotation is used to connect a certain HTTP request method with a controller class or method that will respond to it. This annotation can be used at both levels:
Class level: In this, the request's URL is mapped.
Method level: In this, the URL, as well as the HTTP request method, are mapped.