10X Sale
kh logo
All Courses

Introduction

Spring is an open-source popular application development framework for Java. This framework is used to create high performing, easily testable, and reusable code. When it comes to the size and transparency, Spring is lightweight and its basic version is 2MB. Whether you are a beginner or an intermediate or an expert Spring professional, this guide will boost your confidence and knowledge of Spring.

The questions below cater to various spring related applications and coding along with providing step-by-step explanations for each question that will help you understand the concept better. With Spring interview questions, you can be confident about your preparation for the upcoming interview.

Spring Interview Questions and Answers
Intermediate

1. What is IOC (Inversion of Control)?

This is a frequently asked question in Spring Interview Questions.  

Inversion of control is a software engineering principle that transfers control of objects or parts of a program to a container or framework. In object-oriented programming, it is most often used. In contrast to traditional programming, where our custom code calls to a library, IoC allows a framework to control a program's flow and make calls to our custom code. Frameworks use abstractions with other built-in behaviours to enable this. If we want to add our own behaviour, we must extend the framework classes or add our own classes.

The benefits of this architecture are:

  • Decoupling the implementation of the task
  • It makes switching between different implementations easier
  • Increased program modularity
  • It is easier to test a program by isolating a component or mocking its dependencies and enabling components to communicate via contracts

Various mechanisms such as Strategy design, service locator pattern, factory pattern, and dependency injection (DI) can be used to implement IOC.

2. What is a Component Scan?

When developing Spring Boot applications, tell the Spring Framework where to look for Spring components. A Component scan is one way for Spring to detect managed components from Spring. When the application starts, Spring needs the information to locate and register all Spring components in the application context. Spring can scan, detect and instantiate components from predefined project packages automatically.

Spring can automatically scan all classes with stereotypes @Component, @Controller, @Service and @Repository

For example

import org.springframework.stereotype.Component;

@Component("demoBeanA")
public class DemoBeanA {
}

3. What is @Primary?

A must-know for anyone heading into a Spring interview, this question is frequently asked in Spring framework Interview Questions.  

The @Primary annotation is used in the spring framework to give a bean a higher preference if there are several beans of the same type.

The @Primary annotation can be used directly or indirectly with @Component or with @Bean annotated methods for any class.

The following examples show the use of an annotation @Primary. Consider the Car interface below.

public interface Car {
public void run();
}

Create two beans that implement the user interface named FastCar and SlowCar.

public class FastCar implements Car {

@Override
public void run() {
System.out.println("Inside run() method of FastCar");
}

}

public class SlowCar implements Car {
@Override
public void run() {
System.out.println("Inside run() method of SlowCar");
}
}

In java - based configuration class, declare the FastCar and SlowCar beans.

@Configuration
public class AppConfig {
@Bean
@Primary
public User getFastCar() {
return new FastCar();
}

@Bean
public User getSlowCar() {
return new SlowCar();
}
}

Now create the main class and run the application.

public class MainApp {
public static void main(String[] args) { AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
 Car car=context.getBean(Car.class);
car.run();
context.close();
}
}

Output:

Inside run() method of FastCar

From the above output, it is clear that the getFastCar() (method, which is annotated with @Primary, is first autowired.

4. What is @Qualifier and why it will be used?

When we create more than one bean of the same type and we want to wire only one of them with a property, in that case, we can use @Qualifier with @autowired. Here is an example of the same.

public class UserProfile{
  @Autowired
  @Qualifier("firstUser")
  private User user;

  public UserProfile(){
     System.out.println("Hi" );
  }
  public void printAge() {
     System.out.println("Age of the user: " + user.getAge() );
  }
  public void printName() {
     System.out.println("Name of the user: " + user.getName() );
  }
}

5. What is a ViewResolver?

In order to render models in the browser without binding the implementing to specific view of technology.

View Resolver maps the name of the view to actual views. Spring framework comes with the number of view solvers.

  • InternalResourceViewResolver
  • XmlViewResolver

and a few others.

@EnableWebMvc

@Configuration
@ComponentScan("org.baeldung.web")

public class WebConfig implements WebMvcConfigurer 
      { // All web configuration will go here
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

Spring is an open-source popular application development framework for Java. This framework is used to create high performing, easily testable, and reusable code. When it comes to the size and transparency, Spring is lightweight and its basic version is 2MB. Whether you are a beginner or an intermediate or an expert Spring professional, this guide will boost your confidence and knowledge of Spring. The questions below cater to various spring related applications and coding along with providing step-by-step explanations for each question that will help you understand the concept better. With Spring interview questions, you can be confident about your preparation for the upcoming interview.

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.