Spring Interview Questions


Question: What is DelegatingVariableResolver?
  Question: How can I integrate Java Server Faces (JSF) with Spring?  
Question:  How can I integrate my Struts application with Spring?
Question:  What are the different ways to access Hibernate using Spring ?  
Question:  What are the different ORM’s does Spring supports ?
Question:  How can we integrate Spring and Hibernate using HibernateDaoSupport?
Question:  How can we configure spring in a web application?
Question: Can you have a mycontext.xml file instead of applicationcontext.xml?
Question:  How does Spring relate to MVC framework ?
Question:  How can you setup MessageSources in Spring ?
Question:  What is JdbcTemplate in Spring ?
Question:  Lifecycle interfaces in spring ?
Question: What is inversion of control (IOC) or Dependency Injection?
Question: What are different types of DI?

Question:

What is IoC container of Spring?

Q:

What is DelegatingVariableResolver?

A:

DelegatingVariableResolver is an implementation of custom JSF VariableResolver provided by Spring which extends the standard JSF managed bean mechanism which lets you use JSF and Spring together.

 

Q:

How can I integrate Java Server Faces (JSF) with Spring?

A:

Declare JSF managed-beans in the faces-config.xml configuration file, this will allow the FacesServlet to instantiate that bean at startup. With this your JSF pages now have access to these beans and all of their properties.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">

<faces-config>
  <application>
    <variable-resolver>
        org.springframework.web.jsf.DelegatingVariableResolver
    </variable-resolver>
  </application>
</faces-config>

We can integrate JSF and Spring in two ways:
1) DelegatingVariableResolver : Spring provides 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 WebApplicationContext , Spring's business context. This will allow easy injection of dependencies into JSF-managed beans.

2) FacesContextUtils: The Custom VariableResolver works fine when mapping properties to beans in faces-config.xml, but at times you may need to get a bean explicitly. The FacesContextUtils class makes this task easy. It is almost like WebApplicationContextUtils, except that it takes a FacesContext as parameter instead of ServletContext.

ApplicationContextctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

 

 

Q:

How can I integrate my Struts application with Spring?

A: There are two options to integrate struts application with Spring:
1) Using ContextLoaderPlugin, configure your spring app to manage actions as beans and then set their dependencies in a spring context file.
2) Extend Spring’s ActionSupport classes and get your spring-managed beans using getWebApplicationContext() method.

 

Q:

What are the different ways to access Hibernate using Spring ?

A: There are two ways to integrate Spring and hibernate:
1) By applying Inversion of control of HibernateTemplate and Callback.
2) Extend HibernateDaoSupport and applying an AOP interceptor mechanism.

 

Q:

What are the different ORM’s does Spring supports ?

A: Spring provides the support of various ORMs like Hibernate, iBatis, Jav Persistence Api(JPA), OJB, TopLink,Java Data Objects(JDO).
 

Q:

How can we integrate Spring and Hibernate using HibernateDaoSupport?

A: There is a 3 step process to integrate sparing and hibernate using spring’s SessionFactory known as LocalSessionFactory.
1) Configure Hibernate SessionFactory.
2) Extend HibernateDaoSupport
3) Through AOP wire Transaction support.
 

Q:

How can we configure spring in a web application?

A: To configure your J2EE application to be Spring reach. You need to add Spring’sContextLoaderListener to your web.xml file:
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 

Q:

Can you have a mycontext.xml file instead of applicationcontext.xml?

A:

By default, ContextLoaderListener which is a ServletContextListener gets initialized when your webapp starts up. looks for Spring’s configuration file at WEB-INF/applicationContext.xml. You can change this default value by specifying a <context-param> element named “contextConfigLocation.” Example:

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mycontext.xml</param-value>
      </context-param>
    </listener-class>
  </listener>

 

Q:

How does Spring relate to MVC framework ?

A: In Spring's Web MVC framework : a DispatcherServlet that dispatches requests to handlers.The default handler is a very simple Controller interface, just offering a ModelAndViewhandleRequest(request,response) method.
 

Q:

How can you setup MessageSources in Spring ?

A: By using ResourceBundleMessageSource and the StaticMessageSource
 

Q:

What is JdbcTemplate in Spring ?

A:

JdbcTemplate class is the central class packaged in the JDBC core package.

• It makes usage of JDBC very easy by handling the overhead of creation and release of resources. less chances of most common errors like forgetting to close the connection and leaving application code to only provide SQL and fetch the results.
• It also executes SQL queries, update statements or stored procedure calls, iteration over ResultSets and extraction of returned parameter values.
• It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.

In the bean.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="event" class="com.example.JdbcEventDao">
  <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

</beans>

And In Java class

public class JdbcEventDao implements EventDao {
    private JdbcTemplatejdbcTemplate;
    public void setDataSource(DataSourcedataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

/// DO METHODS
}


 

Q:

Lifecycle interfaces in spring ?

A: 1) InitializingBean
2) DisposableBean

1) InitializingBean
<bean id="initBean" class="com.Spring.InitBean" init-method="init"/>
package com.Spring;
public class InitBean {
public void init() { // do some initialization work }
}

OR Another way,

<bean id="initBean" class=" com.Spring.InitBean "/>
package com.Spring;
public class com.Spring.InitBean implements InitializingBean {
    public void afterPropertiesSet() { // do some initialization work }
}


2) DisposableBean
<bean id="disposeBean" class=" com.Spring.DisposeBean" destroy-method="cleanup"/>
package com.Spring;
public class DisposeBean {
    public void cleanup() {
    // do some destruction work 
    }
}

OR another way,

<bean id=" disposeBean " class=" com.Spring.DisposeBean"/>
package com.Spring;
public class DisposeBean implements DisposableBean {
    public void destroy() { // do some destruction work  }
}

 

Q:

What is inversion of control (IOC) or Dependency Injection?

A: Inversion of control (IOC) or dependency injection (DI) is a design pattern used to give control to the assembler of classes. Generally, if a class wants to use another class, it instantiates desired class. But using this design pattern, the instantiation control is provided to the assembler. Assembler instantiates the required class and injects it in using class.
 

Q:

What are different types of DI?

A: - Constructor Injection
- Setter Injection
- Interface Injection
 

Q:

What is IoC container of Spring?

A: Spring IoC container take care of instantiation of objects, injection of objects in each other and providing enterprise services (e.g. AOP, transaction management) to these objects.



Spring Interview Questions

Similar Topics
Forum For Java Interview Questions new

Java Interview Questions
Java Collection Interview Questions
JSP Interview Questions
Servlet Interview Questions
EJB Interview Questions
JMS Interview Questions
Struts Interview Questions
Spring Interview Questions
WMB Interview Questions
DB Interview Questions

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions