2. Spring Framework Interview Questions

 



Spring Interview Question 



1. What is Spring Framework ?

- The Spring Framework is a open-source, comprehensive framework for building a Java Application. 

- It provides a wide range of features and functionalities that simplify the development of Java applications, particularly in the domain of enterprise and web applications. 

- The framework is known for its modularity, scalability, and ease of integration.

- It was developed by Rod Johnson in 2003.

- Spring is a lightweight framework.

- IoC and DI  is main Features Spring Framework.

- Spring is a Dependency injection framework to make java application loosely couple.

- Spring framework makes the easy development of javaEE (enterprise ) application.



2. What Are the Feature/ Benefits of Using Spring ?

- Lightweight

- Dependency Injection

- IoC Container

- Exception Handling 

- Aspect-oriented programming

- Transaction management

- Data access

- Model-View-Controller(MVC) Framework 

- Web development

- Testing



3. What are the advantages of using Spring Framework ?

- High Productivity: Reduced boilerplate code(Lombok, etc), faster development(auto-config), and simplified testing(JUnit).

- Easy to Maintain: Loose coupling, separation of concerns, and cleaner code structure.

- Security: Dedicated security framework, built-in authentication and authorization, and data protection features.

- Large Community Support: Large and active community support, documentation support, etc.



4. How much types to configure our Spring Application?


1. Java Annotations: Clean and concise, but limited flexibility for large applications.

2. XML Configuration Files: Centralized lengthy configurations, but wordy and less maintainable compared to annotations.

3. Java Configuration Classes: Code-based configuration support

4. Property Sources: Decouples configuration from code, allows dynamic changes but requires additional management.



5. Explain Inversion of Control(IoC) and types of IoC containers.

-

- IoC stands for Inversion of Control.

- Spring IoC is Core of Spring Framework.

- IoC  use to create object and manage the entire life-cycle of that object from creation to destruction. 

- The Container use Dependency Injection(DI) to manage the components of Application.

- The container uses configuration metadata (XML file , Java Annotation) and POJO class for get the information about object/Bean.

- Spring containers are responsible for creating bean objects and injecting them into the classes.

- Since the Controlling of Java objects and their lifecycle is not done by the developers, hence the name Inversion Of Control.  

  


- Types of IoC Containers:

1. Bean Factory: Basic container, that provides basic object creation and dependency injection for spring applications.The default lazy initialization.

2. Application Context: Advanced container, is an implementation of BeanFactory. Can manage object lifecycles, events, and resource access. default Eager initialization. 



6. Explain Dependency Injection(DI) and its types?

- Dependency Injection is way to inject dependencies/Object into Beans/Object by spring Container (IOC).

- Dependency Injection make loose-coupling between the classes.

- Dependency Injection which makes the our code easier to test and increase reusability . 

- What Is a Spring Bean? The Spring Beans are Java Objects that are initialized by the Spring IoC container.


- Types of Dependency Injection-

1. Constructor injection – injection using constructor

2. Setter injection – using setter methods

3. Field injection – directly into the fields


- Which Is the Best Way of Injecting Beans and Why?

- The recommended approach is to use constructor arguments for mandatory dependencies and setters for optional ones. This is because constructor injection allows injecting values to immutable fields and makes testing easier.





7. Explain Spring Beans and their scopes.

-

- Scopes is nothing but Defining lifecycle and  lifetime of a Bean.

- Using Scope we  can control the Bean instance creation. 

- There are five types of spring bean scopes:


1. singleton - only one instance of the spring bean will be created for the spring container. This is the default spring bean scope. single instance share to every call.  

2. prototype – A new instance will be created every time the bean is requested from the spring container.

3. request – This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.

4. session – A new bean will be created for each HTTP session by the container.

5. global-session – This is used to create global session beans for Portlet applications.


8. Are Singleton Beans Thread-Safe?

- No, singleton beans are not thread-safe, as thread safety is about execution, whereas the singleton is a design pattern focusing on creation. Thread safety depends only on the bean implementation itself.



9.  Can We Have Multiple Spring Configuration Files in One Project?

- Yes, in large projects, having multiple Spring configurations is recommended to increase maintainability and modularity.


Ex :

@Configuration

@Import({MainConfig.class, SchedulerConfig.class})

public class AppConfig {

ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");

}


10. Explain Autowiring and its types.

-

- Autowiring reduces the efforts of object instantiation by auto injection of dependencies into beans managed by spring.

- Types of Autowiring:


1. No auto wiring: Setter or constructor-based dependency injection.

2. By name: Matches bean names with property names for injection.

4. By type: Matches bean types with property types for injection.

5. Constructor: Injects dependencies through the bean’s constructor.


11. What are Profiles in Spring Boot

- In Spring Boot, Profiles allow configuration for applications differently in different environments, such as


- Development

- Staging

- Production

- Separate configuration files are defined for each profile, which can be activated using environment variables or command-line arguments.






Spring Web MVC





Comments

Popular posts from this blog

Core Java Interview Question

1. Collection Framework all in one

4. Real-Time Spring boot Interview Question (GenZ Career)