{"id":70419,"date":"2019-02-28T09:10:30","date_gmt":"2019-02-28T09:10:30","guid":{"rendered":"https:\/\/www.whizlabs.com\/blog\/?p=70385"},"modified":"2021-01-21T10:49:43","modified_gmt":"2021-01-21T10:49:43","slug":"spring-framework-interview-questions","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/spring-framework-interview-questions\/","title":{"rendered":"Top 25 Spring Framework Interview Questions"},"content":{"rendered":"<p class=\"p1\" style=\"text-align: justify;\"><em><span class=\"s1\">Aspired to pursue a career in Spring Framework? Or preparing for a Spring Java interview?\u00a0This article will help you to prepare yourself with the Spring Framework Interview Questions during your Spring Java interview preparation.\u00a0<\/span><\/em><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><a href=\"https:\/\/www.whizlabs.com\/blog\/spring-framework-5\/\" target=\"_blank\" rel=\"noopener noreferrer\">Spring Framework<\/a> is the best Java EE Framework for web applications. It is a very lightweight application that offers immense flexibility during runtime. Also, it provides dependency injection and inversion control, which is a useful API for Java programmers. Seeing its rapid growth in popularity and lucrative career prospects, Java programmers need to be more prepared for the Spring interview.<\/span><\/p>\n<p style=\"text-align: justify;\">If you are preparing for a Spring interview, we recommend you go through the top Spring interview questions and prepare well. You can also get a <a href=\"https:\/\/www.whizlabs.com\/blog\/spring-professional-certification-preparation\/\" target=\"_blank\" rel=\"noopener\">Spring certification<\/a> to increase your chances to be considered for the job. You can clear your doubts and gain more knowledge by reading a few good <a href=\"https:\/\/www.whizlabs.com\/blog\/spring-framework-books\/\" target=\"_blank\" rel=\"noopener\">Spring book<\/a>. Let&#8217;s focus on the interview questions here and move to the most common Spring framework interview questions.<\/p>\n<p><strong>Also Check:<\/strong> <a href=\"https:\/\/www.whizlabs.com\/blog\/java-interview-questions-for-freshers\/\" target=\"_blank\" rel=\"noopener\">Java Interview Questions and Answer<\/a><\/p>\n<h2 class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Top 25 Spring Framework Interview Questions<\/span><\/h2>\n<p style=\"text-align: justify;\"><span class=\"s1\">Here we bring the set of top 25 Spring Framework interview questions that will help you crack the interview. Whether you are a beginner or an experienced Java developer, these Spring interview questions will prove an important resource in your interview preparation. So, let&#8217;s get ahead and be ready to ace your Spring Framework interview.<\/span><\/p>\n<h4 class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">1. What is the Spring Framework and why Java Programmers should use it?<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> It\u2019s one of the most common Java Spring Framework interview questions. Spring is one of the most widely used Java EE Framework used by Java Programmers for code development. It provides Dependency Injector, MVC flow, Aspect Oriented Programming, and IOC Container to the Java Programmers. Useful in normal Java applications, it helps to acquire loose coupling in different elements. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">This is done by the implementation of dependency injection that helps perform complex tasks. These tasks may include authentication and logging with spring support for Aspect Oriented Programming. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Being an open source framework with many online resources, it is easy to work with the Spring Framework. It is also known as the frameworks of frameworks because it supports different frameworks like Hibernate, Tapestry, EJB, Struts, JSF, Tapestry, etc. <\/span><\/p>\n<h4><span class=\"s1\">2. What are the features of Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> This is also one of the basic interview questions on Spring Framework. The focus of the Spring Framework is to simplify the process of Java Development. <\/span><\/p>\n<ul class=\"ul1\" style=\"text-align: justify;\">\n<li class=\"li2\"><span class=\"s1\">As Spring Framework is lightweight in terms of size and transparency, it causes a slight overhead in development.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">The Inversion of Control is a Spring Framework container looks after the aspect of writing components that work independently. The Spring Framework container wires them together to fulfill the work.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">The Spring MVC Framework is useful in creating web applications or restful web services. This subsequently helps to return XML and JSON responses.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">The Aspect Oriented Programming in Spring Framework separates the business logic from system services to support cohesive development.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">The IoC container can manage the Spring Bean lifecycle and configurations related to project like JNDI lookup.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">It provides support for transaction management, exception handling, file uploading, etc. \u00a0with Java annotations or XM configuration file of Spring Bean.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">It simplifies the error handling strategy with JDBC extraction layer of Spring Framework that has an exception hierarchy.<br \/>\n<\/span><\/li>\n<\/ul>\n<h4 class=\"p3\" style=\"text-align: justify;\"><span class=\"s1\">\u00a03.\u00a0<\/span><span class=\"s1\">What are the benefits of using Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> If you\u2019ve done browsing earlier to find some popular Spring Framework interview questions, then you have certainly come across this question. <\/span><\/p>\n<ul class=\"ul1\" style=\"text-align: justify;\">\n<li class=\"li2\"><span class=\"s1\">The layered architecture in Spring Frameworks, you can use and leave components as per your preferences.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">No vendor can lock-in as it is an open source.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">It consists of Plain Old Java Object programming that helps in continuous integration and testability.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Simplified JDBC because of the dependency injection and IoC (Inversion of Control).<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">It\u2019s a powerful open source framework that reduces the complex process of enterprise application development.<br \/>\n<\/span><\/li>\n<\/ul>\n<h4><span class=\"s1\">4. What is the meaning of Dependency Injection?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> It\u2019s an aspect of Inversion of Control (IoC), which states that there is no need to create objects manually. Instead, it is important to mention how they are created. \u00a0These design patterns help to remove the hard-coded dependencies and instead it injects dependency on Object. Dependency Injection makes unit testing easy, separation of concerns, boilerplate reduction and configurable components. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">In this process, objects need to define their dependencies. The container injects these dependencies and creates the bean. The bean then controls the instantiation by using Service Locator or direct construction classes. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">The advantage of using dependency injection is that it gives the configuration flexibility. The one thing, which is fixed, is the client\u2019s behavior. It does not demand any changes in code behavior can be used as refactoring in legacy code. \u00a0The high cohesion in DI help maximizes module reusability and minimizes module complexity. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">While some of its disadvantages include difficulty in tracing the code, explosion of types, tight coupling and requiring configure details via construction code. If you know the whole thing about dependency injection, you\u2019 realize later that most of your Spring Framework interview questions will be based on this topic only.<\/span><\/p>\n<h4><span class=\"s1\">5. How can we implement DI (Dependency Injection) in Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>This question adds to a list of Spring Framework interview questions and answers for experienced<b> <\/b>Java Programmers. For implementing DI in Spring Framework applications, we can use both Spring XML based and Annotation based configuration. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><strong><span class=\"s1\">Dependency injection with Spring XML<\/span><\/strong><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">A new Java project with de.vogella.spring.di.xml configuration should be created. Then add the minimum spring jars into the classpath. You have to copy the model class from the de.vogella.spring.di.model project and paste into this project. Create a folder with name META-INF under the src folder and create your spring configuration file in this folder. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Configuring the DI in a spring framework with annotations is easy. First, create a Java project de.vogella.spring.di.annotations.first and add the minimum spring jars into the classpath. Then you have to copy the model class from the new project just created into this project. Now you will need to add annotations to inform spring which beans require to be connected and managed. \u00a0@Service annotation should be added to the NiceWriter.java and MySpringBeanWithDependency.java. Define @Autowired on the setWriter that the Spring will autowire the property writer. <\/span><\/p>\n<h4><span class=\"s1\">6. What is @Autowired Annotation?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>The objective of @Autowired annotation is to give accurate control to know the location and method of autowiring. This is a type driven injection by default and find its uses in the autowire bean, constructor, etc. In case of dependency injection, @Autowired will first inform spring to look for a spring bean that will implement the required interface and put it into the setter. The @Autowired annotation is applied to the following:<\/span><\/p>\n<ul class=\"ul1\" style=\"text-align: justify;\">\n<li class=\"li2\"><span class=\"s1\">@Autowired on property<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">@Autowired on constructor<br \/>\n<\/span><\/li>\n<li class=\"li1\"><span class=\"s1\">@Autowired on the setter method<br \/>\n<\/span><\/li>\n<\/ul>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">You can activate this annotation by including &lt;context:annotation-config \/&gt; tag in the configuration file.<\/span><\/p>\n<blockquote>\n<p style=\"text-align: justify;\">Even when you are preparing for a Spring Java interview, the fundamental and advanced knowledge in various aspects of Java is recommended. So, check out these top 100 <a href=\"https:\/\/www.whizlabs.com\/blog\/top-java-interview-questions\/\" target=\"_blank\" rel=\"noopener noreferrer\">Java Interview Questions<\/a> and get fully prepared for the interview.<\/p>\n<\/blockquote>\n<h4><span class=\"s1\">7. What is Inversion of Control (IoC)?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>IoC is a principle or a point during which the flow of the program is inverted. Rather, the external sources like framework and services and programmer control the flow of the program. As the name, Inversion of Control means inverting the control to get the loose coupling and creates the object with the new operator to container or framework. Now it\u2019s the responsibility of the container to create an object as required.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">In loose coupling, the @Autowired annotation over the reference and Spring Framework takes control to inject the dependency. While in tight coupling, the responsibility to create the dependency is on dependent class. This is again an important question that you\u2019ll definitely find in your upcoming Spring Framework interview questions. <\/span><\/p>\n<h4><span class=\"s1\">8. What is Spring Bean and what is its scope?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Spring Bean is a normal java class, which is initialized using the Spring IoC container. Spring ApplicationContext is used to fetch the Spring Bean instance. It is assembled, instantiated and managed by a Spring IoC container. Spring beans are created by using configuration metadata that you will pass to the container.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">There are three ways to supply the configuration metadata to the Spring IoC container. These include XML-based configuration file, annotation-based configuration, and Java-based configuration. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Spring Framework supports the five scopes of a Spring Bean<\/span><\/p>\n<ul class=\"ul1\" style=\"text-align: justify;\">\n<li class=\"li2\"><span class=\"s1\">Singleton- uses only one bean instance for each container. In this scope, Spring Bean should not have shared instance variables as that may cause data inconsistency.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Session- uses a new bean for each HTTP session.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Global-session- creates global session beans for use in Portlet applications.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Prototype- creates a new instance every time with the request for the bean.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\"> Request- similar to prototype scope but used for web applications<br \/>\n<\/span><\/li>\n<\/ul>\n<h4 class=\"p3\" style=\"text-align: justify;\"><span class=\"s1\">\u00a09.\u00a0<\/span><span class=\"s1\">How can we inject beans in Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>As this is a little practical concept, this question is often seen in the Spring Framework interview questions and answers for experienced users.<\/span> <span class=\"s1\">Options like setter injection, constructor injection and field injection exists that helps to inject beans in the Spring Framework. However, the configuration is possible using XML file or annotations.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><b>Setter-based injection<\/b><\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>The setter injection happens in a situation when container asks for a setter method on your beans. This occurs after it has invoked a no-argument static factory method or a no-argument constructor to instantiate that bean. Here is an example showing a class that can only have pure setter injection.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">For example:<\/span><\/p>\n<pre class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">public class SimpleStudentList {<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ the SimpleStudentList has a dependency on StudentFind<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0private StudentFind studentFind;<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ a setter method that Spring container can 'inject' a StudentFind<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0public void setStudentFind(StudentFind studentFind ) {<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.studentFind = studentFind ;<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ business logic<\/span>\r\n\r\n<span class=\"s1\">}<\/span><\/pre>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><b>Constructor-based injection<\/b><\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">This occurs when the container calls a constructor with a number of arguments, representing a dependency or other class. To construct the bean, particular arguments invoke a static factory method. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">For example:<\/span><\/p>\n<pre class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">public class SimpleStudentList {<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ the SimpleStudentList has a dependency on StudentFind<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0private StudentFind studentFind;<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ a constructor that Spring container can 'inject' a StudentFind<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0public SimpleStudentList(StudentFind studentFind ) {<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.studentFind = studentFind ;<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0\u00a0\/\/ business logic code<\/span>\r\n\r\n<span class=\"s1\">}<\/span><\/pre>\n<h4><span class=\"s1\">10. What is Spring Bean Lifecycle?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Based on the definition of Java or XML, first, there is a need to create a spring instance. Spring Beans may require initialization to have it in a usable state. Once there is no requirement of bean, it is removed from the IoC container. Spring Framework helps in post-initialization and pre-destruction methods in Spring Beans. As this is the most popular Spring Framework Interview Questions among interviewers, we\u2019re providing you with more information.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Once the bean is destroyed, there may be a need for some cleanup activity for the given bean. The collection of these activities is called bean lifecycle. <\/span><\/p>\n<h4><span class=\"s1\">11. What is the significance of Spring Bean configuration file?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Spring bean configuration file defines all the beans that are initialized by Spring Context. It reads the XML file of spring bean when there is a need to create the Spring ApplicationContext instance. After the initialization of context, we can utilize it to get various bean instances. Besides Spring bean configuration, the spring bean configuration file also consists of view resolvers, spring MVC interceptors and other aspects that can support annotations based configurations. <\/span><\/p>\n<h4><span class=\"s1\">12. What is view Resolver Pattern and how it works in Spring MVC?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>It is often that interviewers ask this question as it is assumed to be one of the most popular Spring Framework interview questions. It is a J2EE pattern that allows a web application to select its view technology dynamically. For example, it can use XSLT, JSF, HTML or any other technologies.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">View resolver provides the accurate mapping of different views, along with the controller return name of the view, which then passes to view resolver to choose an appropriate view.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">To choose the views, Spring MVC framework supplies with inbuilt view resolver as well. <\/span><\/p>\n<h4><span class=\"s1\">13. What is Spring Security?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Formerly known as Aegis Security, Spring Security is a framework that aims to provide both authorization and authentication in Java applications. It provides out-of-the-box support to create login screens, securing URL, LDAP and in memory, remember me cooking support and much more. Through the use of annotations such as @EnablewebSecurity, it becomes easier to use Spring Security in web applications. <\/span><\/p>\n<h4><span class=\"s1\">14. Does spring bean provide thread safety in Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Singelton is the default scope of Spring Bean. This is why there is only one instance per context. It means that they have a class level variable that is upgradable by any thread. Thus, it can create data inconsistency, which suggests there is no thread safety in spring beans. Spring container only takes care of the lifecycle of the object, and it also assures that there is only one object in spring container.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Thus, injection of the non-thread safe object is completely not threaded safe. You have to deal with it by coding to make it thread-safe. In case, it\u2019s a web application; you can achieve the thread safety with scope (\u201crequest\u201d). As for every new request, it creates a new object or scope (\u201cprototype\u201d). It creates a new bean for each invocation. Though the answer to this question is very easy, still, some of the interviewers like to add it to their list of Spring Framework interview questions.<\/span><\/p>\n<blockquote><p>Also Read: <a href=\"https:\/\/www.whizlabs.com\/blog\/top-java-frameworks\/\" target=\"_blank\" rel=\"noopener noreferrer\">Top 5 Java Frameworks<\/a><\/p><\/blockquote>\n<h4><span class=\"s1\">15. What is a Controller in Spring MVC?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Similar to the MVC design pattern, Controller is also a class that focuses on client requests and handles them in configured resources. The class which initializes the context as per the spring beans configurations; the front controller class in Spring MVC is org.springframework.web.servlet.DispatcherServlet. It is responsible for handling different types of client requests according to the request mappings. The given question is usually in the list of Spring Framework interview questions for experienced users. <\/span><\/p>\n<h4><span class=\"s1\">16. How to enable transactions in spring and what are their benefits?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>The two different ways to enable or configure transactions includes- Aspect Oriented Programming or annotations. Spring Transactions offer a programming model, which remains consistent through different transaction APIs like JTA, Hibernate, JDO, JPA, and JDC. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">It integrates very well with different data access abstractions in Spring Framework. Another benefit is that it supports declarative transaction management. Also, it offers a simpler API to deal easily with programmatic transaction management. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Here is an example on how to enable transaction with spring XML configuration, using JPA transaction API and namespace support:<\/span><\/p>\n<pre class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">&lt;bean id=\"txManager\" class=\"org.springframework.orm.jpa.JpaTransactionManager\"&gt;<\/span>\r\n\r\n<span class=\"s1\"> \u00a0\u00a0&lt;property name=\"entityManagerFactory\" ref=\"myEmf\" \/&gt;<\/span>\r\n\r\n<span class=\"s1\">&lt;\/bean&gt;<\/span>\r\n\r\n<span class=\"s1\">&lt;tx:annotation-driven transaction-manager=\"txManager\" \/&gt;<\/span><\/pre>\n<h4><span class=\"s1\">17. How to create ApplicationContext in a Java Program?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>This question usually comes up in a quick discussion of top spring framework interview questions. Below are the ways to create ApplicationContext in a standalone java program: <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">ClassPathXmlApplicationContext- If the XML file is of spring bean configuration in a standalone application, then this class play its role in loading the file and get the container object.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">AnnotationConfigApplicationContext- This is used to initialize the container and get the objects in the bean. It comes to play its part when we use annotations for Configuration and Spring Framework in standalone Java applications. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">FileSystemXmlApplicationContext- Likewise ClassPathXmlApplicationContext, this class also uses the spring bean configuration in standalone application. There is just one difference that the XML configuration file can be loaded from any location in the file system.<\/span><\/p>\n<h4><span class=\"s1\">18. What is the relation between Spring MVC Framework and MVC architecture?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Built on top of the Model-View-Controller architecture, Spring MVC uses the DispatcherServlet as its front controller. It looks after all the incoming requests and then delegates these requests to different controller handler methods. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Spring MVC framework is similar to other MVC framework in the way that it offers form data in automatic binding to java beans. View pages use these model beans as attributes. \u00a0These view pages may include static HTML, JSP, etc. However, view resolvers hold the responsibility to find the correct view page. After the identification of the view page is made, the control is handed over back to the DispatcherServlet controller. This is simple yet one of the best Java Spring Framework interview questions that never show its absence in the interviews.<\/span><\/p>\n<h4><span class=\"s1\">19. What do you mean by Spring DAO?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Spring DAO provides its support to work with data access technologies like JDO, JDBC, Hibernate, Oracle and Common Client Interface. There is no need to look for specific exceptions as Spring DAO provides consistency in the exception hierarchy. This makes it easier to shift between persistence technologies. Also, it allows one to code without any worry to catch exceptions that are related to each of these technologies. It helps in the integration of object-relational mapping objects and Java database connectivity. DAO classes throw SQLException in Spring Throw. The benefit of using DAO module is that it becomes simple to keep database code clean with DAO module. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Furthermore, it also helps to prevent any issues that may arise due to poor handling of database resources closures. To enable objects in Spring Framework application and use transaction management services, DAO module uses AOP module. <\/span><\/p>\n<h4><span class=\"s1\">20. How to integrate Spring Framework and Hibernate Frameworks?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>To integrate the Spring Framework and Hibernate Frameworks, Spring ORM module can be used. But, if you use Hibernate 3+, then try to avoid HibernatedaoSupport or HibernateTemplate classes. Rather use DAO pattern that provides dependency injection for integration. Moreover, Spring ORM gives support to use Spring Framework declarative transaction management. So, rather than choosing the code of hibernating boiler-plate, use the Spring Framework declarative transaction management. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">This is a somewhat practical question, in contrary to other spring framework interview questions.<\/span><\/p>\n<h4><span class=\"s1\">21. How to achieve transaction management in Spring?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:\u00a0<\/strong>Programmatic transaction management and declarative transaction management are the two methods that support Spring Framework. The commonly used method is Declarative transaction management as it is easy to use and compatible with most of the cases. For declarative transaction method, the @Transactional annotation is used. Then the configuration of transaction manager for DataSource is done in the spring bean configuration file. <\/span><\/p>\n<pre class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">&lt;bean id=\"transactionManager\" class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\"&gt;<\/span>\r\n\r\n<span class=\"s1\">&lt;property name=\"dataSource\" ref=\"dataSource\" \/&gt;<\/span>\r\n\r\n<span class=\"s1\">&lt;\/bean&gt;<\/span><\/pre>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">As this question is very common in interviews, the interviewee should not forget to prepare this question with other Spring Framework interview question and answers.<\/span><\/p>\n<blockquote><p>New to the Spring Framework? Let&#8217;s begin with the quick <a href=\"https:\/\/www.whizlabs.com\/blog\/spring-framework-5\/\" target=\"_blank\" rel=\"noopener noreferrer\">introduction to Spring Framework 5<\/a>.<\/p><\/blockquote>\n<h4><span class=\"s1\">22. What is the difference between @Controller, @Component, @Service and @Repository annotations in Spring?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer: <\/strong>The difference between<strong>\u00a0<\/strong><\/span><span class=\"s1\">@Controller, @Component, @Service,\u00a0and @Repository can be given as follows:<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Controller- The @Controller annotation represents a Spring Web MVC controller as a class. Dependency injection receives the automatically imported beans marked with a @Controller annotation. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Component- For any Spring-managed component, @Component is a generic stereotype. The spring component-scanning mechanism now gets it and put it into the application context. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Service- @Service annotation is a unique feature of a component annotation. This annotation does not add any additional behavior to the @Component annotation. To define intent in a better way, the @Service annotation can be used over @Component in service-layer classes. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Repository- Having similar use and functionality as @Component annotation, @Repository annotation offers extra benefits for DAOs. This annotation makes it easier for unchecked exceptions to translate into Spring DataAccessException. \u00a0<\/span><\/p>\n<h4><span class=\"s1\">23. What are DispatcherServlet and ContextLoaderListener?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong>\u00a0 As a core part of the\u00a0Spring Web MVC framework, DispatchServlet deals with all the HTTP requests and responses. It receives the handler mapping request from the configuration file and forwards it to the controller. DispatcherServlet checks the view resolver entry in the configuration file and then call the specified view component. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">The ContextLoaderListener first creates a context for root web-application context and then place it in the ServletContext. One of its most important functions is to integrate the ApplicationContext lifecycle to the ServletContext. It also automates the creation of ApplicationContext. \u00a0<\/span><\/p>\n<h4><span class=\"s1\">24. List some important Spring Framework annotations?<br \/>\n<\/span><\/h4>\n<p class=\"p3\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> This is one of the most important Spring Framework interview questions that interviewer asks to judge the basic knowledge of a candidate. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Qualifier- When multiple instances are present in bean type, then the @Qualifier annotation is used with a @Autowired annotation to avoid confusion. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@RequestMapping- As this is a very useful annotation, you need to know of some Annotation examples of Spring MVC RequestMapping. @RequestMapping is used to configure URI mapping in controller handler methods. <\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@ResponseBody- @ResponseBody annotation is used to send data as a response XML or JSON.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@Scope- It is used to configure the spring bean scope.<\/span><\/p>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">@ComponentScan, @Configuration and @Bean- These are the annotations generally used for configurations based on java. <\/span><\/p>\n<h4><span class=\"s1\">25. What are some of the best practices for Spring Framework?<br \/>\n<\/span><\/h4>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\"><strong>Answer:<\/strong> Here are some of the best practices for Spring Framework:<\/span><\/p>\n<ul class=\"ul1\" style=\"text-align: justify;\">\n<li class=\"li2\"><span class=\"s1\">Based on the concerns like spring-security.xml and spring-jdbc.xml, the division of spring bean configuration is possible.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">To ensure there are latest configurations, avoid using version numbers in schema reference.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Avoid autowiring and try to configure bean dependencies.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Use only the modules that you need. Do not keep any extra dependencies that are added during the creation of projects via Spring Tool Suite Templates.<br \/>\n<\/span><\/li>\n<li class=\"li2\"><span class=\"s1\">Don\u2019t use dependency injection just for the sake of loose coupling, as it will become difficult for you to maintain. Just use it for actual benefit.<br \/>\n<\/span><\/li>\n<li class=\"li1\"><span class=\"s1\">To understand the purpose easily, use the proper annotations for components.<br \/>\n<\/span><\/li>\n<\/ul>\n<h3 class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">Final Words <\/span><\/h3>\n<p class=\"p1\" style=\"text-align: justify;\"><span class=\"s1\">So here we&#8217;ve covered the top interview questions on Java Spring Framework. Hope, this post on Spring Framework interview questions and answers will help you to ace your upcoming spring interview. If you know some more interview questions on Spring Framework, make sure to add it in a comment. We&#8217;d be happy to cover them in this article with the detailed answers.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\">Getting a certification also proves as an add-on in your interview preparation. If you are aspired to achieve a better job as a Java developer, you can validate your skills and knowledge with a Java certification. Check out our <a href=\"https:\/\/www.whizlabs.com\/oracle-java-certifications\/\" target=\"_blank\" rel=\"noopener noreferrer\">Oracle Java certifications training that will help you become a certified Java professional.<\/a><\/p>\n<p style=\"text-align: justify;\">Have any questions regarding Java Spring Framework? Ask us in the comment below, we&#8217;ll be happy to answer you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aspired to pursue a career in Spring Framework? Or preparing for a Spring Java interview?\u00a0This article will help you to prepare yourself with the Spring Framework Interview Questions during your Spring Java interview preparation.\u00a0 Spring Framework is the best Java EE Framework for web applications. It is a very lightweight application that offers immense flexibility during runtime. Also, it provides dependency injection and inversion control, which is a useful API for Java programmers. Seeing its rapid growth in popularity and lucrative career prospects, Java programmers need to be more prepared for the Spring interview. If you are preparing for a [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":70749,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[1798,1799,1800,1801,1802],"class_list":["post-70419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-best-spring-framework-interview-questions","tag-interview-questions-on-spring-framework","tag-java-spring-framework-interview-questions","tag-popular-spring-framework-interview-questions","tag-spring-framework-interview-questions-and-answers-for-experienced"],"uagb_featured_image_src":{"full":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1-150x150.png",150,150,true],"medium":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1-300x158.png",300,158,true],"medium_large":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"large":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"1536x1536":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"2048x2048":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"profile_24":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",24,13,false],"profile_48":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",48,25,false],"profile_96":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",96,50,false],"profile_150":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",150,79,false],"profile_300":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",300,158,false],"tptn_thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1-250x250.png",250,250,true],"web-stories-poster-portrait":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",600,315,false],"web-stories-publisher-logo":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",96,50,false],"web-stories-thumbnail":["https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2019\/02\/Spring_Framework_Interview_Questions-1.png",150,79,false]},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":4,"uagb_excerpt":"Aspired to pursue a career in Spring Framework? Or preparing for a Spring Java interview?\u00a0This article will help you to prepare yourself with the Spring Framework Interview Questions during your Spring Java interview preparation.\u00a0 Spring Framework is the best Java EE Framework for web applications. It is a very lightweight application that offers immense flexibility&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/70419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=70419"}],"version-history":[{"count":4,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/70419\/revisions"}],"predecessor-version":[{"id":77132,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/70419\/revisions\/77132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media\/70749"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=70419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=70419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=70419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}