Friday, January 6, 2012

spring interview questions


  • What is IOC (or Dependency Injection)?


The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created
You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file


  • What is Spring ?

Spring is an open source framework created to address the complexity of enterprise application development.
Spring is a lightweight inversion of control and aspect-oriented container framework.



  • What are the advantages of Spring framework?

Spring has layered architecture.
Use what you need and leave you don't need now.
Spring Enables POJO Programming.
 POJO programming enables continuous integration and testability.
Dependency Injection and Inversion of Control Simplifies JDBC


  • What are features of Spring ?
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.

MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality.
This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI.
But other frameworks can be easily used instead of Spring MVC Framework.

JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy.
Integration with Hibernate, JDO, and iBATIS:
Spring provides best Integration services with Hibernate, JDO and iBATIS



  • What are the different modules in Spring framework?

The Core container module
Application context module
AOP module (Aspect Oriented Programming)
JDBC abstraction and DAO module
O/R mapping integration module (Object/Relational)
Web module
MVC framework module

Reference:
http://www.developersbook.com/spring/interview-questions/spring-interview-questions-faqs.php
http://www.javabeat.net/articles/103-spring-framework-interview-questions-1.html




  • Model-view-controller framework

The Spring Framework features its own MVC web application framework, which wasn't originally planned. The Spring developers decided to write their own web framework as a reaction to what they perceived as the poor design of the (then) popular Jakarta Struts web framework,
Like Struts, Spring MVC is a request-based framework
MVC paves the way for cleaner front end code. All interfaces are tightly coupled to the Servlet API.
This tight coupling to the Servlet API is seen by some as a failure on the part of the Spring developers to offer a high-level abstraction for web-based applications[citation needed].
However, this coupling makes sure that the features of the Servlet API remain available to developers while offering a high abstraction framework to ease working with said API.
http://en.wikipedia.org/wiki/Spring_Framework#Model-view-controller_framework



  • With desktop Java apps, you can have a single piece of 

code that instantiates the container and gets beans
• I.e., driver class that calls instantiates
ClassPathXmlApplicationContext and calls getBean
– With Web apps, each servlet wants access to beans
• But you want to instantiate container once only


bean scopes
– Standard Spring supports singleton and prototypeStandard Spring supports singleton and prototype
– Web apps also want request, session, and application


Regular Web Apps
Put bean definitions in WEB-INB/applicationContext.xml
• request and session scope now supported• request and session scope now supported
Declare two listeners in web.xml
• Container will be instantiated when app is loaded

JSF Apps
Same approach for JAR files, bean defn file, and listenerspp , ,
– Declare variable-resolver in faces-config.xml
Can declare beans in applicationContext or faces-config



applicationContext.xml
– Put “empty” file (with header and py (
<beans..></beans> only) in WEB-INF
• Unlike in desktop apps, the filename matters. The
standard loading utility assumes that name and location

/WEB-INF/applicationContext.xml
• If you want to change this default name/location, set a
context param called contextConfigLocation to override it


ContextLoaderListener
– This listener runs when the app is firs
instantiates the ApplicationContext (f
WEB-INF/applicationContext.xml) and places aWEB INF/applicationContext.xml) an
reference to it in the ServletContext
You can retrieve this reference with the static
tR i dW bA li ti C t t th d fgetRequiredWebApplicationContext method of
WebApplicationContextUtils


RequestContextListener
– This listener is needed if you declare any of your beans to
be request-scoped or session-scoped
IWb itdfth lSi f• I.e., Web scopes instead of the usual Spring scopes of
singleton or prototype


Not good to call getBean
– It would be technically legal to get the
ApplicationContext and call getBean explicitly (probably
from the backing bean’s action controller method). Butfrom the backing bean s action controller method). But
this is a bad idea since JSF is geared around declaring
beans in config files only



JSF already supports dependency injection
– The managed-property element lets you insert other beans
inside newly created ones.inside newly created ones.
• The only trick is to be able to refer to Spring beans



Use DelegatingVariableResolver
– Declare in faces-config.xml. Now, whenever JSF sees a
bean name, it uses JSF rules first, then Spring rules next.



Regular Web Apps
• Two JAR files
– Put Spring JAR files in WEB-INF/lib
• Bean definition file
PliiC liWEB INF– Put applicationContext.xml in WEB-INF
• Scopes now include request and session in addition to
singleton and prototype
• Listeners
– Two listener definitions in web.xml
G tti b• Getting beans
– Access ApplicationContext with static
getRequiredWebApplicationContext method ofgetRequiredWebApplicationContext method of
WebApplicationContextUtils
– Call getBean normally



JSF-Based Apps
• Basic setup
– Start with same setup as regular Web apps
– Use normal JSF definition of FacesServlet in web.xml
faces config xml• faces-config.xml
– Declare DelegatingVariableResolver
• Option 1• Option 1
– Declare Spring beans in applicationContext.xml
– Declare backing beans in faces-config.xmlg g
• Refer to Spring beans with managed-bean-property
• Option 2
– Declare all beans in applicationContext.xml
• Refer to other beans with ref and normal Spring syntax



Gets the Spring bean called sampleLookupService
and passes it to the setLookupService method of the
JSF backing bean called formBean (i.e., injects it
into the lookupService property).
into the lookupService property).


03-Spring-in-Web-Apps.pdf
http://courses.coreservlets.com/Course-Materials/spring.html

No comments:

Post a Comment