Using Spring in Java Web application

by lichen 1/29/2008 2:55:00 PM

I have had some fun with the dependency injection feature of the Spring framework. With dependency injection, class does not have to know their collaborators at compile time. Objects are connected to each other at run time via spring configuration file. The result is losely coupled application. Spring makes testing easier since one can inject mock objects by modifying the configuration file. The following is what I learnt:

  1. It is easiest to start with singleton objects. Singleton was often implemented with static classes. However, static classes has lots limitations such as static class cannot implement an interface and cannot be inherited. To avoid these limitation, one can go with various factory patterns. People from asp.net background often use Provider pattern. To use provider pattern, once often need to create configuration handler. With spring, once can use a generic configuration file, making spring much easier to use than provider pattern. With spring, one can use any class as singleton without losing any OO features.
  2. Code to interface. Create interfaces and then implement them. Code the client to consume the interface instead of the implementation. This way, different implementations can be easily injected.
  3. Constructor injection vs property injection. Use constructor to inject required service. Use property to inject optional service.

So much for the theory. Now let us see some code:

  1. The following spring configuration define some singleton objects. Each object is identified by an id.
  2. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" value="jdbc/myDataSource" />

    </bean>

    <bean id="mySessionFactory"

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="myDataSource"/>

    <property name="mappingResources">

    <list>

    <value>MyStoredProc.hbm.xml</value>

    </list>

    </property>

    <property name="hibernateProperties">

    <value>hibernate.dialect=org.hibernate.dialect.DB2Dialect</value>

    </property>

    </bean>

    <bean id="myHibernateTemplate"

    class="org.springframework.orm.hibernate3.HibernateTemplate">

    <property name="sessionFactory">

    <ref bean="mySessionFactory"/>

    </property>

    </bean>

    </beans>

    In the above config file, we use bean element to define bean and use ref attribute to inject one bean into another bean. Spring provides a few helper classes to work with Hibernate.

  3. In web applications, the spring configuration file can be loaded using a servlet listener anc context parameters. Spring provides several listeners, such as org.springframework.web.util.Log4jConfigListener and org.springframework.web.context.ContextLoaderListener. One can load the listener with the following snippets from the web.xml file:
  4. <context-param>

    <description>

    </description>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/spring-config/spring-*.xml</param-value>

    </context-param>

    <listener>

    <description>

    </description>

    <display-name>Log4jConfigListener</display-name>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

    </listener>

    <listener>

    <description>

    </description>

    <display-name>ContextLoaderListener</display-name>

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

    </listener>

  5. Spring context loader loads the context into the servlet context. One can retrieve the spring context from the servlet context. However, if a class does not have access to servlet context, we can create the following helper class:
  6. public class SpringContext implements ApplicationContextAware {
     private static ApplicationContext ctx;
     
     /* (non-Javadoc)
      * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
      */
     public void setApplicationContext(ApplicationContext appCtx) {
      ctx = appCtx;
     }  public static ApplicationContext getContext() {
      return ctx;
     }
    }

    Note that class implements ApplicationContextAware interface. This allows spring to automatically inject application context to this class. This can be done with the following configuration:

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

    <beans>

    <bean id="springContext" class="acme.SpringContext" />

    </beans>


  7. Lastly, I will show one example of constructor injection:
  8. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="jbossCache" class="acme.JBossCache" >

    <constructor-arg type="java.lang.String" value="treecache.xml"/>

    </bean>

    </beans>

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Java

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

1/5/2009 7:43:35 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  January 2009  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Pages

    Recent comments

    Authors

    Tags

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      © Copyright 2009

      Sign in