Clouds
Home

Test Driven Development

Aspect Oriented Programming

Annotation Based Programming

Inversion Of Control - Part 1

Inversion Of Control - Part 2

Hibernate Interview Questions - Part 1

Hibernate Interview Questions - Part 2

Hibernate Interview Questions - Part 3

Spring Interview Questions

EJB 3.0 Interview Questions

Spring Interview Questions


Give an overview of the Spring framework ? What are the benefits of Spring framework ?

The Spring framework is the leading full-stack Java/J2EE application framework. Unlike other frameworks, Spring does not impose itself on the design of a project due to its modular nature and, it has been divided logically into independent packages, which can function independently. It provides a light weight container and a non-invasive programming model enabled by the use of dependency injection (aka IoC), AOP (Aspect Oriented Programming), and portable service abstractions (JdbcTemplate, JmsTemplate etc).It includes abstraction layers for transactions, persistence frameworks (e.g. HibernateTemplate support for Hibernate), Web development, a JDBC integration framework, an AOP integration framework, email support, Web Services (i.e. JAX-RPC) support etc. It also provides integration modules for popular Object-to-Relational (O/R) mapping tools like Hibernate, JDO etc. The designers of an application can feel free to use just a few Spring packages and leave out the rest. The other spring packages can be introduced into an existing application in a phased manner. Spring is based on the IoC pattern (aka Dependency Injection pattern) and also complements OOP (Object Oriented Programming) with AOP (Aspect Oriented Programming). You do not have to use AOP if you do not want to and AOP complements Spring IoC to provide a better middleware solution.

Benefits of Spring framework:

  • Spring can effectively organize your middle tier objects as shown in the diagram above, whether or not you choose to use EJB. Applications built using Spring are easy to test. For example: As shown in the diagram above with the shaded area, your business logic and data access logic can be easily tested outside the container by injecting (i.e. dependency injection. Spring supports constructor and setter dependency injections) business objects and DAO objects into your JUnit test cases and later on if remote access is required, can be deployed inside the EJB container with a thin layer (i.e. no business logic) of EJBs (i.e. stateless session beans for remote access). Spring also provides support for proxying remote calls via RMI, JAX-RPC etc.
  • Spring can facilitate good programming practice by encouraging programming to interfaces rather than to implementation.
  • Spring provides a consistent framework for data access, whether using JDBC or O/R mapping frameworks like Hibernate, TopLink or JDO. Spring ORM support for hibernate reduces the code size by almost a half and provides additional benefits such as easier testing, consistent exception hierarchy (Spring folds your SQLException to a common set of unchecked exceptions) and management of Hibernate resources like SessionFactory. Spring exceptions are unchecked and hence you do not have to write any try{} catch{} and finally{} semantics and also you can manage the transactions declaratively via Spring transaction management using Spring AOP
  • Spring provides a consistent abstraction for transaction management by supporting different transaction APIs such as JTA, JDBC, Hibernate, iBATIS and JDO. Supports both programmatic transaction management and declarative transaction management (preferred approach for transaction management since it has least impact on application code due to its non-invasive nature). Unlike EJB, Spring does not have a default support for distributed transactions (i.e. XA transactions - ) but can plug-in a JTA transaction manager.
  • JDBC applications are verbose with try{}, catch{} and finally blocks and very tedious to write. A good abstraction layer like Spring lets you customize a default JDBC with a query and anonymous inner class to eliminate much of the code. You do not have to worry about managing the resources like DataSource, Connection, Statement and ResultSet, configuring your DataSource, managing transactions and SQLExceptions. Spring IoC + AOP (for declarative transaction) will take care of all these.

    Hibernate persistence code without and with Spring

    Without Spring ORM support

    public List getCustomers() throws MyCheckedException {
    List customers = null;
    Session session = null;
    try {
    session = sessionFactory.openSession();
    customers = session.find("from Customer ");
    }
    catch(Exception ex){
    //handle exception
    }
    finally{
    session.close();
    }
    return customers;
    }

    With Spring ORM support

    public List getCustomers ( ) {
    return getHibernateTemplate().find ("from Customer ");
    }

    What is the important consideration if you are using Spring declarative transaction management with EJB (i.e. for remote access) ?

    You need to turn off the EJB transaction support by setting the transaction attribute to "NotSupported"

    <container-transaction >
    <method >
    <ejb-name>CRMService</ejb-name>
    <method-name>*</method-name>
    </method>
    <trans-attribute>NotSupported</trans-attribute>
    </container-transaction>


    JDBC persistence code without and with Spring IOC

    Without Spring IOC

    DataSource ds = (DataSource) getLocatorInstance().getService("jdbc/Names");
    Connection con = ds.getConnection();
    Statement stmt = null;
    ResultSet rs = null;
    final List listNames = new ArrayList(20);
    try{
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT user.name as name From User user");
    while(rs.next()){
    names.add(rs.getString("name"));
    }
    }
    catch(Exception ex){
    //handle exception code.
    }
    finally{
    try{
    if(rs != null) rs.close()
    if(stmt != null) stmt.close();
    if(con != null) con.close();
    }
    catch(Exception ex){
    //handle exception code
    }
    }
    With Spring IOC support JdbcTemplate template = new JdbcTemplate(dataSource);
    final List listNames = new ArrayList(20);
    template.query("SELECT User.name as name FROM User" ,new RowCallbackHandler () {
    public void processRow (ResultSet rs) throws SQLException {
    names.add(rs.getString("name"));
    }
    });


    EJB 3.0 Interview Questions >>>




    Home Clouds