Important Interview Questions About Spring

Q11: What do you mean by Dependency Injection?

Ans: With Dependency Injection, there is no need for you to create your objects. Nevertheless, you need to give a description of how you want them to be created. Here, the developer doesn’t connect the components and services together directly in the code. Instead of that, he has to describe the services that are required by the components in the configuration file. After that, the IoC container is going to wire them up together.

It is a key aspect of the Spring framework that injects objects into dependencies or other objects. In other words, this enables the loose coupling of components.

Q12: How many bean scopes are supported by Spring?

Ans: In total, there are five scopes that are supported by the Spring Framework.

  • Singleton: This gives scope for the definition of the bean to a single instance per IoC container in Spring.
  • Prototype: By this, the object gets scope for a single bean definition to have any preferable number of object instances.
  • Request: This gives the scope by which a bean is defined as an HTTP request.
  • Session: In the session, the bean can be defined as an HTTP session.
  • Global-session: This gives the scope of a Global HTTP session from the bean definition.

Remember, if you use a web-aware ApplicationContext, only then you can use the last three.

Q13: Describe Spring Security?

Ans: This module is kind of separate from the Spring framework. The aim of Spring security is to give authentication and authorization techniques in Java applications. Not only that, it handles frequent security vulnerabilities like CSRF attacks.

This type of authentication is powerful and customizable. This access-control framework is the de-facto standard that ensures safety to the Spring-based applications. In order to use the Spring Security framework in any of the web applications, there is a simple way to get started. All you have to do is to use a basic annotation .i.e, @EnableWebSecurity, and you are good to go.

Q14: State the difference between BeanFactory and ApplicationContext?

Ans: There are huge differences between BeanFactory and ApplicationContext. The example to highlight their difference is everywhere on the internet. However, we will briefly tell you the points that make them different from one other.

BeanFactory is a simple container with insubstantial functionality. It doesn’t occupy too much space which can be a positive feature of this container. Due to the limited functions, you can utilize it to perform basic tasks or when you are working on low-resource machines. We suggest using BeanFactory when you have less memory left on the system.

The ApplicationContext is an advanced container with an improved interface and added capabilities, such as AOP. hence, you can use it for the task that requires more functionality.

Q15: Explain Spring Beans? And how they are different from Java beans.

Ans: It won’t be incorrect to say that beans are the backbone of the user’s application. As mention previously, the Spring IoC container is responsible for managing them, instantiating, wiring them together, and configuring them. The configuration metadata that the developer sends to the container creates beans.

On the other hand, Java Bean is serializable and is not managed by the Ioc container. Interestingly, at the same time, a Java object may exist in the form of a JavaBean, a POJO, and a Spring bean. This is something that a Spring bean is not able to do.