Thursday, April 6, 2023

Spring Interview Questions : 2023



Spring Interview Questions




Spring Interview Questions : 2023


1.What is Spring in Java?

  1. Spring is open-source, application framework used for enterprise application development.
  2. It is lightweight and loosely coupled.
  3. In the spring framework many predefined templet, use for faster development.
  4. It is easy to test.

2.Name of the Module's given in Spring.

  1. Spring Context :- It is use for DI.
  2. Spring MVC :- It is use for creating web application, web services etc.
  3. Spring ORM :- for ORM tool support such as Hibernate.
  4. Spring AOP :- It is use for Aspect Oriented Programming.
  5. Spring DAO :- For database operations using DAO pattern.
Module's given in Spring




3.What is Spring MVC in Java?

  1. Spring MVC it is module in Spring .
  2. It is a provides Model-view-controller architecture to develop flexible and loosely coupled web application.
Model :- Application data(POJO Class).
View :- HTML page seen by the end user.
Controller :- Responsible for processing user request and building model and passes it to the view for                         rendering.

4.What is Dispatcher Servlet?

  1. It is the front controller.
  2. It is handles all the incoming HTTP requests and send back the response to the Clint.
Dispatcher Servlet




5.What is Spring IoC Container and its Advantages?

  1. The spring IoC container (Inversion of Control) is at core of the Spring Framework.
  2. This Container read the configuration file and based on that it will create and store the objects and manages it this process called Io
  3. C container.
The main tasks performed by IoC container are :

  1. to instantiate the application class
  2. to configure the object
  3. to assemble the dependencies between the objects


Advantages :
Developers can concentrate on business logic since object creation and managing are handled by IoC containers.

6.What are the Types of IoC containers in Spring?

  1. BeanFactory:- It is the interface that contains collection of beans. It instantiates them whenever asked by clients.
  2. ApplicationContext :- It is built on top of the BeanFactory interface. It provides some extra functionality compared to BeanFactory.

7.Diffrence Between BeanFactory And ApplicationContext?

Beanfactory

ApplicationContext

1.BaenFactory is also known as Basic IoC Container. 1.ApplicationContext is also known as Advanced IoC Container.
2.It is an interface defined in org.springframework.beans.factory.Beanfactory 2.It is an interface defined in org.springframework.context.ApplcationContext
3.It uses Lazy initialization 3.It uses Eager/ Aggressive initialization
4.Internationalization is not supported by BeanFactory. 4.Internationalization is supported by ApplicationContext.
5.It doesn't supports annotation based dependency 5.It supports annotation based dependency

8.How To Create ApplicationContext in project?

In order to create an ApplicationContext in a project, you will need to follow these steps:
  1. Choose a dependency injection framework: There are several popular dependency injection frameworks available in Java, such as Spring, Guise, and Dagger. Choose the one that best fits your project requirements.
  2. Add the framework to your project: Add the framework to your project's dependencies using a build tool like Maven or Gradle.
  3. Configure the framework: Each framework has its own configuration mechanism. You will need to configure the framework to tell it how to create and manage objects in your application.
  4. Create a configuration class: In the configuration class, you can define the objects that should be created by the framework and how they should be created. This is typically done using annotations or XML configuration.
  5. Create the ApplicationContext: Once you have defined your configuration class, you can create the ApplicationContext. This is typically done by creating a new instance of the framework's ApplicationContext class and passing in your configuration class.

Here's an example using the Spring framework:



// 1. Choose Spring framework 
// 2. Add Spring to your project dependencies 
// 3. Configure Spring 
@Configuration 
public class AppConfig {
@Bean 
public MyService myService()
return new MyServiceImpl(); 
 } 
// 4. Create configuration class 
// 5. Create ApplicationContext 
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

In this example, we've chosen the Spring framework, added it to our project's dependencies, defined a configuration class (AppConfig) that defines a bean (MyService), and created an ApplicationContext using AnnotationConfigApplicationContext. The ApplicationContext will manage the lifecycle of the MyService bean and any other beans defined in the configuration class.

9. Types of  ApplicationContext?

  1. AnnotationConfigApplicationContext: This type of ApplicationContext is used when you want to configure your application using annotations. It scans your project's classes for annotations and automatically configures the beans based on those annotations.
  2. ClassPathXmlApplicationContext: This type of ApplicationContext is used when you want to configure your application using an XML file. It loads the XML file and creates the beans defined in it.
  3. FileSystemXmlApplicationContext: This type of ApplicationContext is similar to ClassPathXmlApplicationContext, but it loads the XML file from the file system instead of the classpath.
  4. WebApplicationContext: This type of ApplicationContext is used in web applications. It provides integration with web-specific features such as servlets, filters, and listeners.
  5. AnnotationConfigWebApplicationContext: This type of ApplicationContext is used when you want to configure your web application using annotations. It's similar to AnnotationConfigApplicationContext, but provides additional support for web-specific features.
  6. XmlWebApplicationContext: This type of ApplicationContext is similar to ClassPathXmlApplicationContext, but provides additional support for web-specific features.

10.What is Spring Bean?

  1. Any normal class that is initialized by IoC Container called as a Spring Bean.
  2. Spring IoC container manages the life cycle of Spring Bean, bean scope and injecting any required dependencies in the bean. 

11. How many bean scopes are supported by Spring?

  1. Singleton :- only one instance of bean is created for each container.(default)
  2. Prototype :- A new instance created every time the bean is requested.
  3. Request :-  A new instance of the bean will be created for each HTTP request.
  4. Session :- A new instance of bean created will be for each HTTP session.
  5. Global-session :- This is used to create global session beans.
Spring Bean Scopes




To set spring bean scopes we can use "scope" attribute in bean element or @Scope annotation for annotation based configuration.

12.What is the Spring Bean life Cycle?

The Spring container is responsible for managing the life cycle of a bean in a Java program. When a program is executed, the container is the first to be started, and it creates an instance of a bean as per the request. Dependencies are then injected, and finally, the bean is destroyed when the Spring container is closed. To execute custom code during bean instantiation or after the Spring container is closed, developers can write their code in the custom init() and destroy() methods.

Spring Bean life Cycle






13.What is Spring Configuration file and how many way we can do it?

A spring configuration file is used to define all the beans that will be initialized by spring context. Once the context is initialized, we can use it to get different bean instances.

we can configure Spring Beans through the below ways :
  1. XML Configuration :- (XML file).
  2. Java Based Configuration :- (@Configuration).
  3. Annotation Based Configuration :- @Component, @service, @Repository and @Controller.

14.What do you mean by Dependency Injection?


DI is the process of passing/injecting dependency object to the dependent object.
Example : Class Car , Class Engine
Car :- dependent object , Engine :- dependency object .

Dependency Injection





14. Types Of Dependency Injection .

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection

15.Diffrence Between Setter and Constructor Injection.


Constructor Injection

Setter Injection

1.No Partial Injection 1.Partial Injection
2.Desn't override the setter property 2.Overrides the constructor property if both are defined.
3.Creates new instance if any modification occurs 3.Doesn't create new instance if you change the property value
4.Better for too many properties 4.Better for few properties.

16.What is Bean Wiring?

  1. The process of injecting spring bean dependencies while initializing it called spring bean wiring.
  2. This can be achieved by using @Autowired annotation.

17.What are  Autowiring Modes?

  1. no :- this is the default mode, it means autowiring is not enabled.
  2. byName :- injects the bean based on the property name. It uses setter method.
  3. byType :- injects the bean based on the property type. It uses setter method.
  4. constructor :- It injects the bean using constructor
Autowiring Modes




18.What is an InternalResourceViewResolver in Spring MVC?

  1. ViewResolver implementations are used to add Prefix and Suffix of the view files locations.
  2. It is configured using spring bean configuration file.
  3. InternalResourceViewResolver is one of the implementation of ViewResolver interface.
Prefix and Suffix

19.What is @ComponentScan annotation in Spring?

  1. The process of scanning Spring Components with in classpath of the application is called Component scanning in Spring.
  2. Later spring will use them to add in application context(IoC container).
  3. @ComponentScan annotation tells Spring that where to look for Spring Components explicitly. 
  4. @ComponentScan annotation is used with @Configuartion annotation.
  5. @Component, @Controller, @Repository, @Configuration annotated classes will be treated as Spring Components.

20.In which scenario, you will use singleton and prototype scope?

Singleton scope should be used with EJB stateless session bean and prototype scope with EJB stateful session bean.


21.What is AOP?

  1. AOP is stand for Aspect Oriented Programming. It is a methodology that divides the program logic into pieces or parts or concerns.
  2. It increases the modularity and the key unit is Aspect.

22.What does @Controller annotation?

The @Controller annotation marks the class as controller class. It is applied on the class.


23.What does @RequestMapping annotation?

The @RequestMapping annotation maps the request with the method. It is applied on the method.

24.What is Advice?

Advice represents action taken by aspect.


25. What are the types of advice in AOP?

  1. Before Advice
  2. After Advice
  3. After Returning Advice
  4. Throws Advice
  5. Around Advice
types of advice in AOP



Conclusion

Spring interviews are an important part of the recruitment process for companies that use Spring in their development projects. By asking specific questions related to the framework, employers can gauge the candidate's level of expertise and experience in Spring development. It is essential for candidates to have a solid understanding of core Spring concepts such as dependency injection, inversion of control, and aspect-oriented programming. Additionally, candidates should be familiar with the various modules of Spring such as Spring MVC. "Good Luck" !!!!!!!!!!.




 

No comments:

Post a Comment