You can check all 87 Spring interview questions here π https://devinterview.io/dev/spring-interview-questions
Spring is an open source development framework for enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based (Plain Old Java Object) programming model.
The act of creating associations between application components (beans) within the Spring container is referred to as Bean wiring.
Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods in Java applications. It also takes care of most of the common security vulnerabilities such as CSRF attacks.
To use Spring Security in web applications, you can get started with a simple annotation: @EnableWebSecurity.
The bean definition contains the information called configuration metadata which is needed for the container to know the followings β
- How to create a bean
- Bean's lifecycle details
- Bean's dependencies
Spring Boot is a project that provides a pre-configured set of frameworks to reduce boilerplate configuration so that you can have a Spring application up and running with the smallest amount of code.
The DispatcherServlet
is an implementation of the Front Controller design pattern that handles all incoming web request to a Spring MVC application. A Front Controller pattern (see Enterprise application design pattern) is a common pattern in web applications whose job is to receive all request and route it to different components of the application for actual processing.
In Spring MVC, DispatcherServlet
is used for finding the correct Controller to process a request, which it does with the help of handler mapping, e.g. the @RequestMapping
annotation.
The spring-mvc.jar
is not part of spring-core, which means that if you want to use Spring MVC framework in your Java project, you must include spring-mvc.jar
in your application's classpath. In a Java web application, spring-mvc.jar
is usually placed inside the /WEB-INF/lib
folder.
Following is the list of few of the great benefits of using Spring Framework:
- Lightweight β Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
- Inversion of control (IOC) β Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
- Aspect oriented (AOP) β Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
- Container β Spring contains and manages the life cycle and configuration of application objects.
- MVC Framework β Spring's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over-engineered or less popular web frameworks.
- Transaction Management β Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).
- Exception Handling β Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.
Spring beans are just object instances that are managed by the Spring container, namely, they are created and wired by the framework and put into a "bag of objects" (the container) from where you can get them later.
The "wiring" part there is what dependency injection is all about, what it means is that you can just say "I will need this thing" and the framework will follow some rules to get you the proper instance.
The core container provides the essential functionality of the Spring framework. A primary component of the core container is the BeanFactory
, an implementation of the Factory pattern. The BeanFactory
applies the Inversion of Control (IOC) pattern to separate an application's configuration and dependency specification from the actual application code.
On the surface, an application context is the same as a bean factory. Both load bean definitions, wire beans together, and dispense beans upon request. But it also provides:
- A means for resolving text messages, including support for internationalization
- A generic way to load file resources
- Events to beans that are registered as listeners
JSF and Spring do share some of the same features, most noticeably in the area of IOC services. By declaring JSF managed-beans in the faces-config.xml configuration file, you allow the FacesServlet to instantiate that bean at startup. Your JSF pages have access to these beans and all of their properties.We can integrate JSF and Spring in two ways:
- DelegatingVariableResolver: Spring comes with a JSF variable resolver that lets you use JSF and Spring together. The DelegatingVariableResolver will first delegate value lookups to the default resolver of the underlying JSF implementation, and then to Spring's 'business context' WebApplicationContext. This allows one to easily inject dependencies into one's JSF-managed beans.
- FacesContextUtils: custom VariableResolver works well when mapping one's properties to beans in faces-config.xml, but at times one may need to grab a bean explicitly. The FacesContextUtils class makes this easy. It is similar to WebApplicationContextUtils, except that it takes a FacesContext parameter rather than a ServletContext parameter.
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface. So if a bean implements the ApplicationListener, then every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified.
Spring supports the following ORM's:
- Hibernate
- iBatis
- JPA (Java Persistence API)
- TopLink
- JDO (Java Data Objects)
- OJB
Following are the modules of the Spring framework:
- Core module
- Bean module
- Context module
- Expression Language module
- JDBC module
- ORM module
- OXM module
- Java Messaging Service(JMS) module
- Transaction module
- Web module
- Web-Servlet module
- Web-Struts module
- Web-Portlet module
The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API.
There are following three important methods to provide configuration metadata to the Spring Container β
- XML based configuration file.
- Annotation-based configuration
- Java-based configuration
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/>
definitions.
@RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.
Scope prototype means that every time you call for an instance of the Bean, Spring will create a new instance and return it. This differs from the default singleton scope, where a single object instance is instantiated once per Spring IoC container.
Model is a reference to encapsulate the data or output for rendering. Model is always created and passed to the view in Spring MVC. If a mapped controller method has Model as a method parameter, then a model instance is automatically injected by Spring framework to that method.
A View
is an interface in Spring MVC application whose implementations are responsible for rendering context and exposing the model. A single view exposes multiple model attributes. Views in Spring MVC can be beans.
They are likely to be instantiated as beans by a ViewResolver
. As this interface is stateless, view implementations should be thread-safe. By using ViewResolver
, a logical name of view can be resolved into different types of View implementation, e.g. JstlView for displaying JSP or other view implementations for FreeMarker and Velocity.
Thanks π for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here π Devinterview.io