Spring Framework Interview Questions
What is the Spring Framework?
The Spring Framework is an open-source Java application framework that provides a comprehensive programming and configuration model for building modern enterprise applications.
What are the core features of the Spring Framework?
- Lightweight and Non-intrusive: The Spring Framework achieves loose coupling and maintainability through IoC (Inversion of Control) and AOP (Aspect-Oriented Programming).
- IoC Container: The Spring Framework provides a container responsible for instantiating, configuring, and managing objects within the application.
- AOP Support: The Spring Framework supports Aspect-Oriented Programming to handle cross-cutting concerns such as logging and transaction management.
- Declarative Transaction Management: The Spring Framework offers support for declarative transaction management, simplifying the process of managing database transactions.
- Comprehensive MVC Framework: The Spring Framework provides a flexible and extensible MVC framework for building web applications.
What is Inversion of Control (IoC) and Dependency Injection (DI)?
Inversion of Control is a software design principle that transfers object creation and dependency resolution from the objects themselves to a container, rather than having them manage these internally. Dependency Injection is an implementation mechanism of Inversion of Control where dependencies are injected into objects by the container.
What is AOP in the Spring Framework? What is its purpose?
AOP (Aspect-Oriented Programming) is a key feature of the Spring Framework used to handle cross-cutting concerns within systems. Through AOP, functionality unrelated to business logic (such as logging and transaction management) can be separated from the core application logic, improving code modularity and maintainability.
How is Transaction Management implemented in the Spring Framework?
The Spring Framework provides support for declarative transaction management. By adding transaction annotations (e.g., @Transactional) on methods or classes, Spring intercepts method calls to handle the beginning, commitment, or rollback of transactions.
Spring Beans
What is a Spring Bean?
In the Spring Framework, a Bean is an object that is instantiated, assembled, and managed by the IoC container. A Bean serves as a fundamental building block for Spring applications and can be defined via configuration files or annotations.
How are there different ways to configure beans in Spring?
- Using XML Configuration Files: By defining bean metadata within XML configuration files.
- Using Annotations: By adding annotations (such as
@Component,@Service) on classes to identify Beans, and using annotations (like@Autowired,@Value) for dependency injection and property configuration. - Using Java Configuration Classes: By writing Java configuration classes that use specific annotations (
@Configuration,@Bean) to define and assemble beans.
What scopes does Spring support for beans?
Spring Beans support 5 different Scopes, listed as follows:
- Singleton - There is only one single Bean instance per Spring IoC container. Default scope.
- Prototype - A new instance is created every time a request occurs.
- Request - A new Bean instance is created for each HTTP request, and the Bean remains valid only within that specific HTTP request.
- Session - A new Bean instance is created per Session, remaining valid only within the current HTTP Session.
- Application - A new Bean instance is created per Web Application, remaining valid only within the current Web Application context.
Spring Annotations
What are the differences between @Component, @Controller, @Repository, and @Service?
@Component: Marks a Java class as a Bean. It serves as a generic stereotype for any component managed by Spring.@Controller: Marks a class as a Spring Web MVC Controller.@Service: This annotation is a specialization of the@Componentannotation. It provides no additional behavior over@Component. You can use@Serviceinstead of@Componentin service layer classes because it specifies intent more clearly.@Repository: This annotation is another specialization of the@Componentannotation with similar purposes and functionality. It offers extra benefits for DAOs by importing them into the IoC container, allowing unchecked exceptions to be converted into Spring DataAccessException instances.
What does the @Required annotation do?
The @Required annotation applies to Bean property setter methods.
- This annotation indicates that a bean definition must use an explicit property value during configuration or rely on auto-wiring to populate the affected Bean properties.
- If the affected Bean property has not been populated, the container will throw a
BeanInitializationException.
What does the @Autowired annotation do?
The @Autowired annotation allows for more precise control over where and how automatic wiring should occur.
- This annotation is used to automatically wire Beans into setter methods, constructors, properties of arbitrary names or multiple parameters, and methods.
- By default, it performs type-driven injection.
What does the @Qualifier annotation do?
When you create multiple beans of the same type and wish to use property wiring for only one specific bean, you can eliminate ambiguity by using the @Qualifier annotation in conjunction with @Autowired, specifying which exact Bean should be wired via its ID or name.
Spring Profiles
What is a Spring Profile?
A Spring Profile is a mechanism used to define environment-specific configurations. By utilizing Spring Profiles, different environments (e.g., development, testing, production) can utilize distinct configurations.
How do you define a Spring Profile?
You can define a Spring Profile in your Spring configuration files using the @Profile annotation. The @Profile annotation can be applied at either the class level or method level to specify which specific profile should activate.
How do you activate a specific Spring Profile?
There are multiple ways to activate a Spring Profile:
- At application startup, use the
spring.profiles.activeproperty to specify the profile(s) to activate. For example, activating a "dev" profile via command-line argument-Dspring.profiles.active=dev. - In your application configuration files, you can also set the
spring.profiles.activeproperty. For instance, addingspring.profiles.active=devin anapplication.propertiesorapplication.ymlfile activates the "dev" profile.
How do you use multiple Profile configuration files in a Spring Boot application?
Spring Boot supports automatically loading corresponding configuration files based on the activated profiles. You can create multiple configuration files, each associated with a specific profile. The naming convention is application-{profile}.properties or application-{profile}.yml, where {profile} represents the name of the Profile. Spring Boot will automatically load the appropriate configuration file based on the active profile.
How do you perform conditional configuration in code based on Profiles?
You can use the @ConditionalOnProfile annotation to perform conditional configuration based on profiles. This annotation can be applied at either the class level or method level. Using @ConditionalOnProfile("dev"), you specify that corresponding configurations should only apply if a profile named "dev" is active.
How do you use Spring Profiles in testing?
In tests, you can use the @ActiveProfiles annotation to specify which profiles to activate. For example, @ActiveProfiles("test") activates a profile named "test". This allows you to define different profiles for various test scenarios and apply corresponding configurations accordingly.