0% found this document useful (0 votes)
4 views2 pages

Advanced Spring Interview Questions

Uploaded by

Balti Wissem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Advanced Spring Interview Questions

Uploaded by

Balti Wissem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

100 Advanced Spring Interview Questions and Answers

Core Spring Framework (1-20)

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


All are specializations of @Component with additional semantics:
- @Service: Business logic.
- @Repository: DAO layer and exception translation.
- @Controller: Handles web requests in Spring MVC.
- @Component: General-purpose bean.

How does Spring manage bean life cycle?


Bean lifecycle includes instantiation, dependency injection, @PostConstruct,
afterPropertiesSet(), and optional @PreDestroy.

What are BeanFactory and ApplicationContext?


- BeanFactory: Basic container providing dependency injection.
- ApplicationContext: Advanced container with features like event propagation, AOP, and
internationalization.

How does dependency injection work in Spring?


Spring injects dependencies through constructor injection, setter injection, or field injection.

What is @Qualifier and how is it used?


It resolves ambiguity when multiple beans of the same type exist.
@Autowired
@Qualifier("beanName")
private MyBean myBean;

Spring Boot (21-40)

What is the purpose of the spring-boot-starter modules?


They provide pre-configured dependencies for quick application setup.

How does Spring Boot's auto-configuration work?


Auto-configuration scans the classpath and applies default configurations if required beans
are missing.

What is the role of @SpringBootApplication?


Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

How do you exclude an auto-configuration?


@SpringBootApplication(exclude = {[Link]})
How does Spring Boot handle externalized configuration?
Through [Link], [Link], and environment variables.

Common questions

Powered by AI

The @SpringBootApplication annotation combines three essential annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan. It simplifies the definition of the main class of Spring Boot applications by encapsulating these configurations, thus making it a convenient shorthand. It is often the preferred entry point because it automatically sets up the application context, scans for beans and configurations, and applies necessary auto-configurations, enabling quick startup .

@Component is a general-purpose annotation indicating that a class is a Spring component. @Service is used for classes that contain business logic, essentially a specialization of @Component for service layer beans. @Repository is another specialization intended for DAO (Data Access Object) layers, providing benefits such as exception translation. @Controller is tailored for Spring MVC, handling web requests and returning responses. These distinctions impact their use by signaling the intent and role within the application, guiding Spring in processing the classes appropriately .

Auto-configuration can be selectively excluded by using the "exclude" attribute within the @SpringBootApplication annotation, such as @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}). This approach is useful when default configurations do not align with specific application requirements, or when custom setups are necessary. Scenarios that may necessitate this include integrating with legacy systems, using non-standard configurations, or optimizing application performance by excluding unnecessary settings .

Spring-boot-starter modules simplify the setup by providing a set of curated and pre-configured dependencies for specific functionalities such as web, data access, or messaging. They eliminate the need to manually configure and resolve dependency conflicts, improving productivity. This convenience allows developers to focus more on building application features rather than handling configuration details, thereby accelerating the development process .

Spring Boot's auto-configuration mechanism scans the classpath to identify which libraries are present and applies default configurations for these based on the absence of manually defined beans. This significantly reduces the amount of configuration code developers need to write, thereby accelerating development. By having sensible defaults, it ensures a more seamless integration of necessary components, enhancing the developer experience and making spring boot applications more maintainable .

The Spring Framework manages the bean lifecycle through various stages: instantiation, dependency injection, and custom initialization. After a bean is instantiated, Spring injects dependencies as needed, either through constructor, setter, or field injection. Initialization can include @PostConstruct methods or implementing the InitializingBean's afterPropertiesSet() method. Finally, @PreDestroy or DisposableBean methods are executed before the bean is removed from the context, providing hooks for cleanup processes .

In the Spring Framework, dependency injection can be implemented through constructor injection, setter injection, or field injection. Constructor injection involves passing dependencies through a class constructor. Setter injection provides setter methods for individual properties. Field injection directly injects dependencies using annotations like @Autowired on fields. This flexibility allows developers to choose the most appropriate method based on their design needs .

Spring Boot handles externalized configuration through files like application.properties and application.yml, as well as environment variables. This allows properties to be defined outside the application's code, facilitating easier configuration management. The advantages include the ability to tailor configurations for different environments without changing the actual codebase, supporting better managing configurations across various stages of deployment and reducing the risk of configuration-related issues at runtime .

BeanFactory is the basic container in Spring that provides dependency injection capabilities. ApplicationContext extends BeanFactory with more advanced functionalities like event propagation, internationalization, and AOP support. ApplicationContext also provides mechanisms for resolving text messages, and resources such as files. Choosing ApplicationContext over BeanFactory often makes it easier to build complex applications due to these additional features that support development needs and create a robust environment .

The @Qualifier annotation is used in conjunction with @Autowired to resolve ambiguity when multiple beans of the same type are present. By specifying a bean name in @Qualifier("beanName"), it explicitly instructs Spring to use a particular bean from the available candidates. This enhances the dependency injection process by enabling precise control over which bean is injected, facilitating more precise configurations in complex applications .

You might also like