Interview Questions

Get ready for your next interview with our comprehensive question library

Spring Framework Interview Questions

Filter by Difficulty

1.

Explain Inversion of Control (IoC) and Dependency Injection (DI).

beginner

IoC is a design principle where the control of object creation and management is transferred from the application code to an external container (Spring Container).

DI is the implementation of IoC where dependencies are injected into objects rather than objects creating their dependencies.

// Without DI - tight coupling
public class UserService {
    private UserRepository userRepository = new UserRepository(); // Tight coupling
}

// With DI - loose coupling
public class UserService {
    private UserRepository userRepository;
    
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository; // Dependency injected
    }
}
2.

What are the different types of Dependency Injection in Spring?

beginner

Spring supports three types of DI:

  1. Constructor Injection: Dependencies injected through constructor
  2. Setter Injection: Dependencies injected through setter methods
  3. Field Injection: Dependencies injected directly into fields using @Autowired
// Constructor Injection (Recommended)
@Service
public class UserService {
    private final UserRepository userRepository;
    
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

// Setter Injection
@Service
public class UserService {
    private UserRepository userRepository;
    
    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

// Field Injection
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
}
3.

What is the difference between @Component, @Service, @Repository, and @Controller?

beginner

These are stereotype annotations for different layers:

  • @Component: Generic stereotype for any Spring-managed component
  • @Service: Indicates service layer components (business logic)
  • @Repository: Indicates data access layer components (DAO)
  • @Controller: Indicates presentation layer components (MVC controllers)

All are essentially @Component with semantic meaning. @Repository provides additional exception translation.

4.

What are the different ways to configure Spring applications?

beginner

Spring supports three configuration approaches:

  1. XML-based configuration: Traditional XML files
  2. Annotation-based configuration: Using annotations like @Component, @Autowired
  3. Java-based configuration: Using @Configuration classes
// Java-based configuration
@Configuration
public class AppConfig {
    @Bean
    public UserService userService() {
        return new UserService(userRepository());
    }
    
    @Bean
    public UserRepository userRepository() {
        return new UserRepository();
    }
}
5.

What is @Autowired and how does it work?

beginner

@Autowired enables automatic dependency injection. Spring automatically injects matching beans by type. It can be used on:

  • Constructors
  • Setter methods
  • Fields
  • Method parameters
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository; // Field injection
    
    @Autowired
    public UserService(UserRepository userRepository) { // Constructor injection
        this.userRepository = userRepository;
    }
}
6.

What is Spring Boot and what are its advantages?

beginner

Spring Boot is an opinionated framework that simplifies Spring application development by providing:

  • Auto-configuration: Automatically configures Spring applications
  • Starter dependencies: Pre-configured dependency sets
  • Embedded servers: Tomcat, Jetty, Undertow embedded
  • Production-ready features: Metrics, health checks, externalized configuration
  • No XML configuration: Convention over configuration
  • Standalone applications: JAR files with embedded servers
7.

What are Spring Boot Starters?

beginner

Starters are pre-configured dependency descriptors that include all necessary dependencies for specific functionality:

  • spring-boot-starter-web: Web applications (Spring MVC, Tomcat)
  • spring-boot-starter-data-jpa: JPA with Hibernate
  • spring-boot-starter-security: Spring Security
  • spring-boot-starter-test: Testing (JUnit, Mockito, TestNG)
8.

What is @SpringBootApplication annotation?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
9.

What are the different types of request mapping annotations?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
10.

Explain @PathVariable, @RequestParam, and @RequestBody.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
11.

What is the difference between @Controller and @RestController?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What is Spring Data and what are its key features?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
13.

What is Spring Security and what are its core features?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
14.

Explain authentication vs authorization in Spring Security.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
15.

What is the Spring IoC Container and what are its types?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

Explain the different bean scopes in Spring.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

Explain the Spring Bean lifecycle.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is the difference between @Autowired and @Resource?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is Spring Boot Auto-configuration and how does it work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

Explain Spring Boot application properties and profiles.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 49 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime