Due to its vast features and flexibilities, Spring became the most loved framework for developing enterprise-level Java-based applications. In the following section, we will see what are the most commonly asked interview questions and answers to prepare you for Spring-based interviews.
Spring handles all the infrastructure-related aspects which lets the programmer to focus mostly on application development. A Spring configuration file is basically an XML file that mainly contains the classes information and describes how those classes are configured and linked to each other. The XML configuration files are verbose and cleaner.
Spring container forms the core of the Spring Framework. The Spring container uses Dependency Injection DI for managing the application components by creating objects, wiring them together along with configuring and managing their overall life cycles. The instructions for the spring container to do the tasks can be provided either by XML configuration, Java annotations, or Java code. Note: The last three scopes are available only if the users use web-aware ApplicationContext containers. The IoC container autowires relationships between the application beans.
Spring lets collaborators resolve which bean has to be wired automatically by inspecting the contents of the BeanFactory. Different modes of this process are:. Spring Boot is an open-source, java-based framework that provides support for Rapid Application Development and gives a platform for developing stand-alone and production-ready spring applications with a need for very few configurations.
As per the Spring Boot documentation, the SpringBootApplication annotation is one point replacement for using Configuration, EnableAutoConfiguration and ComponentScan annotations alongside their default attributes. This enables the developer to use a single annotation instead of using multiple annotations thus lessening the lines of code. However, Spring provides loosely coupled features which is why we can use these annotations as per our project needs.
We can use the exclude attribute while using the annotation SpringBootApplication as follows:. If the class is not specified on the classpath, we can specify the fully qualified name as the value for the excludeName. Check out more Interview Questions on Spring Boot here. An advice is the implementation of cross-cutting concerns can be applied to other modules of the spring application. Advices are of mainly 5 types:. This can be done by using the query method of JdbcTemplate.
There are two interfaces that help to do this:. The DispatcherServlet does a lot more than that:. Autowired annotation is meant for the injection of a bean by means of its type along with methods and fields. This helps the Spring framework to resolve dependencies by injecting and collaborating the beans into another bean. For example, consider the below code snippet:. The annotation plays a very important role in binding method parameters to the respective attribute that corresponds to a model.
Then it reflects the same on the presentation page. The role of the annotation also depends on what the developer is using that for. In case, it is used at the method level, then that method is responsible for adding attributes to it. When used at a parameter level, it represents that the parameter value is meant to be retrieved from the model layer. It is also used for configuring the ContextLoaderListener. Whenever the application is deployed, a ContextLoaderListener instance is created by Servlet container which leads to a load of WebApplicationContext.
Here, the annotations are used for notifying the Spring runtime that the class specified with Bean annotation is the provider of beans and the process of context scan needs to be performed on the package com. Next, we will be defining a Figure class component as below:. Spring encounters this Figure class while performing context scan and it initializes the instance of this class by invoking the constructor annotated with Autowired.
The Shape and Dimension instances are obtained by calling the methods annotated with Bean in the SpringAppConfig class. Instances of Engine and Transmission will be obtained by calling Bean annotated methods of the Config class.
For example, consider the below code snippet in the springAppConfig. The index and type arguments are used for resolving conflicts in cases of ambiguity. In cases where both types of dependencies are used, then the setter dependency injection has more preference by considering the specificity nature.
Session scopes are used to create bean instances for HTTP sessions. This would mean that a single bean can be used for serving multiple HTTP requests. The scope of the bean can be defined by means of using scope attribute or using Scope or SessionScope annotations. The annotation is used for indicating that the property of the bean should be populated via autowiring or any explicit value during the bean definition at the configuration time.
For example, consider a code snippet below where we need to have the values of age and the name:. No, the singleton beans are not thread-safe because the concept of thread-safety essentially deals with the execution of the program and the singleton is simply a design pattern meant for the creation of objects.
Thread safety nature of a bean depends on the nature of its implementation. The thread safety can be achieved by changing the scope of the bean to request, session or prototype but at the cost of performance. This is purely based on the project requirements. Repository annotation indicates that a component is used as the repository that acts as a means to store, search or retrieve data.
These can be added to the DAO classes. The dispatcher servlet is instantiated by means of servlet containers such as Tomcat. The Dispatcher Servlet should be defined in web. The Dispatcher Servlet can be defined in web. Here, the load-on-startup tag is 1 which indicates that the DispatcherServlet is instantiated whenever the Spring MVC application to the servlet container. During this process, it looks for the servlet-name-context.
The root application context is loaded using the ContextLoaderListener that belongs to the entire application. Spring MVC allows instantiating multiple DispatcherServlet and each of them have multiple contexts specific to them. They can have the same root context too.
A Dispatcher Servlet knows which controller to call by means of handler mappings. These mappings have the mapping between the controller and the requests. The view requires access to the model to render the output as the model contains the required data meant for rendering. The model is associated with the controller that processes the client requests and finally encapsulates the response into the Model object. BindingResults is an important Spring interface that is within the org.
This interface has a very simple and easy process of invocation and plays a vital role in detecting errors in the submitted forms. However, care has to be taken by the developer to use the BindingResult parameter just after the object that needs validation. For example:. The Spring will understand to find the corresponding validators by checking the Valid annotation on the parameter.
This can be achieved by the HandlerInterceptor interface. These handlers are used for manipulating the model attributes that are passed to the controllers or the views. The Spring handler interceptor can be registered for specific URL mappings so that it can intercept only those requests. The custom handler interceptor must implement the HandlerInterceptor interface that has 3 callback methods that can be implemented:.
The only problem with this interface is that all the methods of this interface need to be implemented irrespective of its requirements. This can be avoided if our handler class extends the HandlerInterceptorAdapter class that internally implements the HandlerInterceptor interface and provides default blank implementations.
The spring-mv. For Java applications, the spring-mvc. It also registers the beans defined in the config file and it scans the annotations within the beans and activates them. Spring MVC does the task of data validation using the validator object which implements the Validator interface. In the custom validator class that we have created, we can use the utility methods of the ValidationUtils class like rejectIfEmptyOrWhitespace or rejectIfEmpty to perform validation of the form fields.
In the fields that are subject to validation, in case of errors, the validator methods would create field error and bind that to the field. This can be done by either implementing the spring-aware interfaces or by using the Autowired annotation. BeanFactory and the ApplicationContext are both Java interfaces.
The difference is that the ApplicationContext extends the BeanFactory. Following are the differences between these two:. The following beans need to be configured in the application:.
The MultipartResolver is used for handling the file upload scenarios in the Spring web application. There are 2 concrete implementations of this in Spring, they are:. Doing this ensures that all the requests handled by the DispatcherServlet have this resolver applied whenever a multipart request is detected. Controllers then cast this request as the MultipartHttpServletRequest interface to get access to the Multipart files. The following diagram illustrates the flow clearly:. To use the servlet container which is configured in the JNDI Java Naming and Directory Interface DataSource, the DataSource bean has to be configured in the spring bean config file and then injected into the beans as dependencies.
Post this, the DataSource bean can be used for performing database operations by means of the JdbcTemplate. During HTTP requests, if the state of the checkbox is unchecked, then HTTP includes the request parameter for the checkbox thereby not picking up the updated selection.
In this article, we have seen the most commonly asked Spring Interview Questions during an interview. Spring is a very powerful framework that allows building enterprise-level web applications. Applications developed using Spring are generally quick, scalable, and transparent.
Knowing Spring ensures that an added advantage is with the developers to progress steadily in their careers too. Tip: We also recommend reading guides posted here. What annotation is used for finding transaction and failing it by complaining no Hibernate session is bound to thread? Before you go! Take this "Spring Interview Questions" interview guide with you.
Download PDF. Enter the name of your college. Computer Science. Information Technology. Mathematics and Computing. Before After Enter company name. Forgot Password. What is Spring Framework? What are the features of Spring Framework? What is a Spring configuration file?
What do you understand by Dependency Injection? Explain the difference between constructor and setter injection? What are Spring Beans? How is the configuration meta data provided to the spring container? What are the bean scopes available in Spring? What do you understand by Bean Wiring. What is autowiring and name the different modes of it? What are the limitations of autowiring?
Spring Boot Interview Questions Explain the advantages of using Spring Boot for application development. Differentiate between Spring and Spring Boot. What are the features of Spring Boot?
What does SpringBootApplication annotation do internally? What is Spring Boot dependency management system? What are the possible sources of external configuration?
Can we change the default port of the embedded Tomcat server in Spring boot? Can you tell how to exclude any package without using the basePackages filter? How to disable specific auto-configuration class? Can the default web server in the Spring Boot application be disabled? What is Spring AOP?
What is an advice? Explain its types in spring. How can you fetch records by Spring JdbcTemplate? What are the two ways of accessing Hibernate by using Spring. What is Hibernate Validator Framework? What is HibernateTemplate class? What is the Spring MVC framework? In other words, can you explain the Spring MVC architecture?
What is the Controller annotation used for? Can you create a controller without using Controller or RestController annotations? What is ContextLoaderListener and what does it do? What are the differences between RequestParam and PathVariable annotations? What is the use of Autowired annotation?
What is the role of ModelAttribute annotation? What is the importance of the web. What is the importance of session scope? What is the importance of Required annotation? Differentiate between the Autowired and the Inject annotations. Are singleton beans thread-safe? How can you achieve thread-safety in beans? What is the significance of Repository annotation? How is the dispatcher servlet instantiated? How is the root application context in Spring MVC loaded?
How does the Spring MVC flow look like? Where does the access to the model from the view come from? Why do we need BindingResults? What are Spring Interceptors? Is there any need to keepspring-mvc. How to get ServletConfig and ServletContext objects in spring bean? Differentiate between a Bean Factory and an Application Context.
How are i18n and localization supported in Spring MVC? What is Primary? What is Qualifier? What are the major features in different versions of Spring? What are new features in Spring Framework 4. What are new features in Spring Framework 5. What are important Spring Modules? What are important Spring Projects? What is the simplest way of ensuring that we are using single version of all Spring related dependencies? Name some of the design patterns used in Spring Framework?
What do you think about Spring Framework? Why is Spring Popular? Can you give a big picture of the Spring Framework?
What is Model 2 architecture? What is Model 2 Front Controller architecture? Can you show an example controller method in Spring MVC? Can you explain a simple flow in Spring MVC?
What is a ViewResolver? What is Model? What is ModelAndView? What is a RequestMapping? What is Dispatcher Servlet? How do you set up Dispatcher Servlet? What is a form backing object? How is validation done using Spring MVC? What is BindingResult? How do you map validation results to your view? What are Spring Form Tags? What is a Path Variable? What is a Model Attribute? What is a Session Attribute? What is a init binder? How do you set default date format with Spring?
Why is Spring MVC so popular? Spring Boot What is Spring Boot? What are the important Goals of Spring Boot? What are the important Features of Spring Boot? Compare Spring Boot vs Spring? What is the importance of SpringBootApplication? What is Auto Configuration? How can we find more information about Auto Configuration? What is an embedded server? Why is it important? What is the default embedded server with Spring Boot?
What are the other embedded servers supported by Spring Boot? What are Starter Projects? Can you give examples of important starter projects? What is Starter Parent? What are the different things that are defined in Starter Parent?
How does Spring Boot enforce common dependency management for all its Starter projects? What is Spring Initializr? What is application. What are some of the important things that can customized in application. How do you externalize configuration using Spring Boot? How can you add custom application properties using Spring Boot? What is ConfigurationProperties? What is a profile?
How do you define beans for a specific profile? How do you create application configuration for a specific profile? How do you have different configuration for different environments?
What is Spring Boot Actuator? How do you monitor web services using Spring Boot Actuator? How do you find more information about your application envrionment using Spring Boot? What is a CommandLineRunner?
How is different from JDBC? What is a JdbcTemplate? What is a RowMapper? What is JPA? What is Hibernate? How do you define an entity in JPA? What is an Entity Manager? What is a Persistence Context? How do you map relationships in JPA? What are the different types of relationships in JPA?
How do you define a datasource in a Spring Context? What is the use of persistence. How do you define transaction management for Spring — Hibernate integration?
Spring Data What is Spring Data? What is the need for Spring Data? What is a CrudRepository? What is a PagingAndSortingRepository? What is Mockito? What is your favorite mocking framework? How do you do mock data with Mockito?
0コメント