Wednesday 9 November 2016

Top 20 Spring Interview Questions Answers 2016

Spring MVC Request flow


1) What is spring framework? Why Java programmer should use Spring framework
Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.

2) What is default scope of bean in Spring framework ?
The default scope of a Spring bean is Singleton scope, you can read this article which explains about all possible scope of a spring bean : What is bean scope in Spring


3) Does Spring singleton beans are thread-safe ?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe.


4) What is dependency Injection?
Dependency Injection is one of the design pattern, which allows injecting dependency on Object, instead of object resolving the dependency.

5. What are benefits of 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, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
  • Aspect oriented (AOP): Spring supports Aspect oriented programming and separates 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.
  • Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
  • Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

6) What is Spring MVC ? Can you explain How one request is processed ?

7) How to you create a controller in Spring ?



8) What is view Resolver pattern ? how it work in Spring MVC

View Resolver pattern is a J2EE pattern which allows a web application to dynamically choose it's view technology e.g. HTML, JSP, Tapestry, JSF, XSLT or any other view technology. In this pattern, View resolver holds mapping of different views, controller return name of the view, which is then passed to View Resolver for selecting an appropriate view.
 Spring MVC framework supplies inbuilt view resolver for selecting views.

9) What is Spring Security ?
Spring security is a project under spring framework umbrella, which provides support for security requirements of enterprise Java projects. Spring Security formerly known as aegis security provides out of box support for creating login screen, remember me cookie support, securing URL, authentication provider to authenticate the user from the database, LDAP and in memory, concurrent active session management support and much more. In order to use Spring security in a Spring MVC based project, you need to include spring-security.jar and configure it in application-Context-security.XML file, you can name it whatever you want, but make sure to supply this to ContextLoaderListener, which is responsible for creating Spring context and initializing dispatcher servlet. You can see Pro Spring Security by Carlo Scarioni to learn more about Spring Security.

10) How do you control concurrent Session on Java web application using Spring Security?
You can use Spring Security to control a number of active session in Java web application. Spring security framework provides this feature out of the box and when enabled , a user can only have one active session at a time. See this Spring Security example to learn more about How to control concurrent user session using Spring security
11) What types of dependency injection is supported by Spring Framework? When do you use Setter and Constructor Injection, pros and cons?
There are 2 types of dependency injection supported by Spring, constructor based injection, and setter-based injection. Both types have their own advantages and disadvantages, you should use Constructor injection when object's dependencies are not optional and they must be initialized with their dependencies. Also use constructor injection if the order of initialization or dependency matters because in Setter based injection you cannot impose any order. Use setter injection when dependencies are optional. See the difference between setter and constructor injection in Spring for more detailed answer.

12) If a user checked in CheckBox and got a validation error in other fields and then he unchecked the CheckBox, what would be selection status in command object in Spring MVC ? How do you fix this issue?
Since during HTTP post, if the checkbox is unchecked than HTTP does include a request parameter for checkbox, which means updated selection won't be picked up. you can use hidden form field, starting with _ to fix this in Spring MVC. quite a tricky question to answer if you are not aware of HTTP POST behavior and Spring MVC.

13) What are different implementations of View interface you have used in Spring MVC?
ULBased View e.g. JSP , JSTLView.
12) What is the difference between ApplicationContext and BeanFactory in Spring framework?

13) How do you call stored procedure by using Spring framework?

14) What does JdbcTemplate and JmsTemplate class offer in Spring?


15) Explain Spring MVC flow with a simple example e.g. starting from Container receives a request and forward to your Java application ?

16) What is the difference in Spring MVC and Spring core?

17) Can you use Spring MVC framework along with Struts ? I have an existing Java MVC application which is based in Struts, Can I migrate that to use Spring MVC ? How ?

18) What is the advantage of Spring MVC framework over Struts 1.0 or Struts 2.0 ? is it worth to convert an existing Struts application to Spring MVC ?

19) How do Spring resolves view returned by ModelAndView class ?


20. What are inner beans in Spring?

When a bean is only used as a property of another bean it can be declared as an inner bean. Spring’s XML-based configuration metadata provides the use of <bean/> element inside the <property/> or <constructor-arg/> elements of a bean definition, in order to define the so-called inner bean. Inner beans are always anonymous and they are always scoped as prototypes.


Share This

0 comments: