Spring Interview Questions : 2023
1.What is Spring in Java?
- Spring is open-source, application framework used for enterprise application development.
- It is lightweight and loosely coupled.
- In the spring framework many predefined templet, use for faster development.
- It is easy to test.
2.Name of the Module's given in Spring.
- Spring Context :- It is use for DI.
- Spring MVC :- It is use for creating web application, web services etc.
- Spring ORM :- for ORM tool support such as Hibernate.
- Spring AOP :- It is use for Aspect Oriented Programming.
- Spring DAO :- For database operations using DAO pattern.
3.What is Spring MVC in Java?
- Spring MVC it is module in Spring .
- 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?
- It is the front controller.
- It is handles all the incoming HTTP requests and send back the response to the Clint.
5.What is Spring IoC Container and its Advantages?
- The spring IoC container (Inversion of Control) is at core of the Spring Framework.
- This Container read the configuration file and based on that it will create and store the objects and manages it this process called Io
- C container.
- to instantiate the application class
- to configure the object
- 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?
- BeanFactory:- It is the interface that contains collection of beans. It instantiates them whenever asked by clients.
- 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:
Here's an example using the Spring framework:
- 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.
- Add the framework to your project: Add the framework to your project's dependencies using a build tool like Maven or Gradle.
- 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.
- 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.
- 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);
9. Types of ApplicationContext?
- 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.
- 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.
- FileSystemXmlApplicationContext: This type of ApplicationContext is similar to ClassPathXmlApplicationContext, but it loads the XML file from the file system instead of the classpath.
- WebApplicationContext: This type of ApplicationContext is used in web applications. It provides integration with web-specific features such as servlets, filters, and listeners.
- 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.
- XmlWebApplicationContext: This type of ApplicationContext is similar to ClassPathXmlApplicationContext, but provides additional support for web-specific features.
10.What is Spring Bean?
- Any normal class that is initialized by IoC Container called as a Spring Bean.
- 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?
- Singleton :- only one instance of bean is created for each container.(default)
- Prototype :- A new instance created every time the bean is requested.
- Request :- A new instance of the bean will be created for each HTTP request.
- Session :- A new instance of bean created will be for each HTTP session.
- Global-session :- This is used to create global session beans.
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.
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 :
- XML Configuration :- (XML file).
- Java Based Configuration :- (@Configuration).
- 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 .
14. Types Of Dependency Injection .
- Constructor Injection
- Setter Injection
- 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?
- The process of injecting spring bean dependencies while initializing it called spring bean wiring.
- This can be achieved by using @Autowired annotation.
17.What are Autowiring Modes?
- no :- this is the default mode, it means autowiring is not enabled.
- byName :- injects the bean based on the property name. It uses setter method.
- byType :- injects the bean based on the property type. It uses setter method.
- constructor :- It injects the bean using constructor
18.What is an InternalResourceViewResolver in Spring MVC?
- ViewResolver implementations are used to add Prefix and Suffix of the view files locations.
- It is configured using spring bean configuration file.
- InternalResourceViewResolver is one of the implementation of ViewResolver interface.
19.What is @ComponentScan annotation in Spring?
- The process of scanning Spring Components with in classpath of the application is called Component scanning in Spring.
- Later spring will use them to add in application context(IoC container).
- @ComponentScan annotation tells Spring that where to look for Spring Components explicitly.
- @ComponentScan annotation is used with @Configuartion annotation.
- @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?
- AOP is stand for Aspect Oriented Programming. It is a methodology that divides the program logic into pieces or parts or concerns.
- 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?
- Before Advice
- After Advice
- After Returning Advice
- Throws Advice
- Around Advice
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