Spring Interview Questions


Question: How to instantiate IoC container?
  Question: How does a web application use Spring’s configuration xmls?  
Question:  How to integrate multiple bean configuration xmls?
Question:  What is autowiring?  
Question:  What are different modes of autowiring?
Question:  What are different bean scopes available to configure?
Question:  What is default scope in Spring?
Question: How can you control bean instantiation process?
Question:  How can you control bean destruction process?
Question:  How do you implement inheritance in bean definition?
Question:  Difference between FileSystemResource and ClassPathResource ?
Question:  How to customize bean destruction process ?
Question: How to customize bean initialization process ?
Question: How to implement inheritance in Bean definition ?
or
What is the use of parent attribute in Spring Bean ?

Question:

Explain inner bean in Spring ?
or
What is inner bean in Spring ?
or
What are inner beans in Spring ?

Q:

How to instantiate IoC container?

A:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“services.xml”, “daos.xml”});

 

Q:

How does a web application use Spring’s configuration xmls?

A:

Spring container/configuration xmls can be integrated with web application through web.xml. Following entries in web.xml can integrate Spring container with web container.

  <context-param>
    <description>
        Context parameter to integrate Spring and Web containers
    </description>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath: services.xml,
        classpath: daos.xml
    </param-value>
  </context-param>
  

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

 

Q:

How to integrate multiple bean configuration xmls?

A: Multiple bean configuration xmls are created to separate configurations according to layers so that it becomes easy to manage and maintain them. These configuration xmls can be imported in single xml to combine all of them.

<beans>
<import resource="services.xml"/>
<import resource="daos.xml"/>

<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>

 

Q:

What is autowiring?

A: By Autowiring, Spring injects dependencies without having to specify those explicitly. Spring inspects bean factory contents and establishes relationships amongst collaborating beans. To implement it, just add autowire property in xml configuration.
 

Q:

What are different modes of autowiring?

A: Autowiring has following five modes
- no: No autowiring.
- byName: Autowiring by property name, means bean matching property name is autowired.
- byType: Bean having type as that of property type is autowired.
- constructor: Similar to byType just that the property is in constructor.
- autodetect: Spring is allowed to select autowiring from byType and constructor.
 

Q:

What are different bean scopes available to configure?

A: Following scopes can be assigned to different beans.
- singleton: One bean instance per IoC container
- prototype: Any number of instances of bean
- request: Within HTTPRequest object scope
- session: As long as HttpSession is alive
- globalsession: Within life-cycle of global HttpSession. Applicable in portlet context usually.
 

Q:

What is default scope in Spring?

A: Singleton
 

Q:

How can you control bean instantiation process?

A:

Bean instantiation by Spring can be controlled using initialization call backs. There are two ways of doing it. First is having a initialization method (say init()) and specifying it in bean configuration as ‘init-method’ property. Second is implementing InitializingBean interface and implementing afterPropertiesSet() method in it.

 

Q:

How can you control bean destruction process?

A: There are two ways of doing it. First is add a destroy() method and specify it in bean configuration as ‘destroy-method’ property. Second is implement DisposableBean interface and implement destroy() method of it.
 

Q:

How do you implement inheritance in bean definition?

A: Bean definition inheritance can be implemented by specifying ‘parent’ property of the bean equal to its parent bean definition id. This bean class must have extended itself from the parent bean class.
 

Q:

Difference between FileSystemResource and ClassPathResource ?

A:

In FileSystemResource you need to give path of spring-config.xml (Spring Configuration) file relative to your project or the absolute location of the file.

In ClassPathResource spring looks for the file using ClassPath so spring-config.xml should be included in classpath. If spring-config.xml is in “src” so we can give just its name because src is in classpath path by default.

Exmple:

If in our project we put the spring-config.xml in src/com.springorg then our FileSystemResource would be:

FileSystemResource resource = newFileSystemResource("src/com/springorg/spring-config.xml");

And ClassPathResource would be:

ClassPathResource resource = newClassPathResource("com/springorg/spring-config.xml");

 

Q:

How to customize bean destruction process ?

A:  

Destruction callbacks, Lifecycle callbacks, DisposableBean, destroy(), destroy-method

The Spring offers several callback interfaces to modify the behaviour of your bean. By implementing DisposableBean interface Spring Container will call destroy() method just before the container is destroyed.

This can be achieved by following two ways

(a) ImplemetDisposableBean interface and provide implementation of destroy() method.

<bean id="disposeBean" class="com.spring.DisposeBean"/>
  public class DisposeBean implements DisposableBean {
  public void destroy() {
    // do cleanup task
  }
}

(b) Usually the above described way of customizing the bean initialization process is avoided as it forces our bean class to implement an interface (DisposableBean) thus couples the client code to Spring Container. Same thing can be achieved by using ‘init-method’ attribute of ‘bean’ tag in Spring configuration file as given below.

<bean id=" disposeBean" class=" com.spring.DisposeBean" destroy-method="cleanup"/>
  public class DisposeBean {
  public void cleanup() {
    // do cleanup task
  }
}


 

Q:

How to customize bean initialization process ?

A:

Initialization callbacks, Lifecycle callbacks, InitializingBean, afterPropertiesSet(), init-method
The Spring offers several callback interfaces to modify the behaviour of your bean. By implementing InitializingBean interface Spring Container will call afterPropertiesSet() method to allow the bean to perform certain initialization tasks after all properties on the bean have been injected by the container .

This can be achieved by following two ways

(a)
ImplemetInitializingBean interface and provide implementation of afterPropertiesSet() method.

<bean id="initBean" class="com.spring.InitBean "/>
  public class InitBean implements InitializingBean {
  public void afterPropertiesSet() {
    // do initialization task
  }
}

(b) Usually the above described way of customizing the bean initialization process is avoided as it forces our bean class to implement an interface (InitializingBean) thus couples the client code to Spring Container. Same thing can be achieved by using ‘init-method’ attribute of ‘bean’ tag in Spring configuration file as given below.

<bean id=" initBean " class=" com.spring.InitBean " init-method="init"/>
  public class InitBean {
  public void init() {
    // do initialization task
  }
}

 

Q:

How to implement inheritance in Bean definition ?
or
What is the use of parent attribute in Spring Bean ?

A:

Inheritance can be implemented by specifying the ‘parent’ attribute in bean and passing id of (parent) bean that you want to inherit .
In the example below Employee bean inherits properties(name, age, sex) of Person bean, no need to specify these properties in child bean (Employee).

  <!--?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-3.0.xsd">
      <bean id="personBean" class="com.spring.Person">
        <property name="name" value="David"/>
        <property name="age" value="21"/>
        <property name="sex" value="male"/>
      </bean>
      <bean id="employeeBean" class=" com.spring.Employee" parent="personBean">
        <property name="company" value="XYZCompany"/>
        <property name="address" ref="addressBean"/>
      </bean>
      <bean id="addressBean" class=" com.spring.Address">
        <property name="city" value="Pune"/>
        <property name="state" value="Maharashtra"/>
      </bean>
  </beans>

 

Q:

Explain inner bean in Spring ?
or
What is inner bean in Spring ?
or
What are inner beans in Spring ?

A:

A bean which is defined or embedded inside the property tag of other bean is called an inner bean. Inner beans cannot be referred by other beans except the enclosing one. In following configuration “addressBean” is an inner bean as it is defined inside the property tag of “employeeBean”.

  <!--?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-3.0.xsd">
      <bean id="employeeBean" class=" com.spring.Employee" parent="personBean">
        <property name="company" value="thecafetechno.com"/>
        <property name="address">
          <bean id="addressBean" class=" com.spring.Address">
            <property name="city" value="Pune"/>
            <property name="state" value="Maharashtra"/>
          </bean>
        </property>
      </bean>
  </beans>




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