Part three: Wiring the backend with Spring Framework and Hibernate

This is part three of the Minesweeper portlet tutorial.

This part of the tutorial describes how we can wire our Struts actions with Spring beans and how we can utilize the Spring dao support with Hibernate.

Saving the highscore to database

The following steps try to illustrate how to combine Struts with Spring and Hibernate for easy data access.
  1. First we create the HighScore class to represent a highscore entry in the database
  2. public class HighScore {
        private Long id;
        private int score;
        private String name;
    }
    
  3. We map it to database table highscore with HighScore.hbm.xml mapping file
  4. <hibernate-mapping>
        <class name="com.blogspot.pragmatastic.minesweeper.HighScore" table="highscore">
            <id name="id" column="id">
                <generator class="native"/>
            </id>
            <property name="score" column="score"/>
            <property name="name" column="name"/>
        </class>
    </hibernate-mapping>
    
  5. Then we create a HighScoreDao class for saving the data with the spring hibernate support
  6. public class HighScoreDaoImpl extends HibernateDaoSupport implements HighScoreDao {
        public void save(HighScore highScore) {
            getHibernateTemplate().save(highScore);
        }
    }
    
  7. Now we create a spring application context file minesweeper-applicationContext.xml
  8. First we define the data source for the database which is configured by db.properties
  9. <beans>
        <bean id="dbConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:db.properties</value>
            </property>
            <property name="placeholderPrefix" value="${db."/>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="${db.url}"/>
            <property name="username" value="${db.username}"/>
            <property name="password" value="${db.password}"/>
        </bean>
    
  10. Then we define the hibernate session factory and list our HighScore.hbm.xml mapping file as resource
  11.     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
                <list>
                    <value>com/blogspot/pragmatastic/minesweeper/HighScore.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                </props>
            </property>
        </bean>
    
  12. And finally we define our dao
  13.     
        <bean id="highScoreDao" class="com.blogspot.pragmatastic.minesweeper.HighScoreDaoImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    </beans>
    
  14. Now we need to make the beans defined in the minesweeper-context.xml available for our struts action. First we configure spring to load the context file and create a application context from it in web.xml
  15. <web-app>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:minesweeper-applicationContext.xml</param-value>
        </context-param>    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>
    
  16. Next we enable the struts spring integration in the struts.xml
  17. <struts>
        <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
    </struts>
    
  18. After that we can refer to the HibernateDao object in HighScoreAction, spring will automatically wire our action with the bean
  19. public class HighScoreAction extends ActionSupport {
        private HighScore highScore;
        private HighScoreDao highScoreDao;
    
        // Auto wired byName from spring
        public void setHighScoreDao(HighScoreDao highScoreDao) {
            this.highScoreDao = highScoreDao;
        }
    
        public HighScore getHighScore() {
            return highScore;
        }
    
        // Saves the form
        public String save() {
            highScoreDao.save(highScore);
            return SUCCESS;
        }
    }
    
Continue to Part four: Making the UI with jQuery and Freemarker

References

http://www.springbyexample.org/examples/simple-hibernate-xml-config.html
http://static.springsource.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html
http://struts.apache.org/2.0.11/docs/spring-plugin.html

No comments:

Post a Comment