For this tutorial we’re going to build a basic blog application and extend it in later tutorials. We’re going to start with the basic setup of a Spring annotations web application.
I’ve made a web module in my project with this layout:
web (project name)
|– src
|—|— main
|—|— |–java
|—|— |–resources
|—|— |–webapp
|—|— |–|–WEB-INF
We’re going to need to setup our application with three files:
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Sandbox</display-name> <!-- To indicate where the context is found --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/app-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--A filter to help add parameters to the request paramters. I'm a fan of the carbonfive implementation. See the reference section below for more details. --> <filter> <filter-name>PathParameterFilter</filter-name> <filter-class>carbonfive.spring.web.pathparameter.ParameterizedPathFilter</filter-class> </filter> <filter-mapping> <filter-name>PathParameterFilter</filter-name> <servlet-name>spring</servlet-name> </filter-mapping> <!-- The item below declare a Dispatcher Servlet * named spring and tell the application that all requests should go to the spring servlet. (It could have been named anything, doesn't have to be named 'spring'.) --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
* See references at the end of this post
Since we’re going to hook Hibernate into Spring, it’s easier to get Spring set up first.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=""> <!-- Put bean locations here --> <!-- Annotation setup --> <context:annotation-config /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- URL Mapping --> <bean class="carbonfive.spring.web.pathparameter.ParameterizedUrlHandlerMapping"> <property name="alwaysUseFullPath" value="false" /> <property name="interceptors"> <list> <ref bean="hibernateInterceptorSessionInView" /> </list> </property> <property name="defaultHandler" ref="homeController"/> <property name="mappings"> <props> <prop key="/index">homeController</prop> </props> </property> </bean> <!-- Our controllers --> <bean id="homeController" class="com.tutorial.sandbox.web.HomeController"> <property name="viewName" value="index" /> </bean> <!-- Wiring to work with Freemarker --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/" /> <property name="freemarkerSettings"> <props> <prop key="tag_syntax">auto_detect</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".html" /> <property name="requestContextAttribute" value="rc" /> <property name="exposeSpringMacroHelpers" value="true" /> </bean> </beans>
Then we hook up hibernate.
In the xml file you will see that the hibernate.hbm2ddl.auto property is set to auto. This means that any changes to our entities will automatically be applied to our database. Remember to make sure that your database exists.
Remember that this was referenced in the spring-servlet.xml file.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=""> <!-- Put bean locations here--> <context:annotation-config /> <!-- Setting up Hibernate --> <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.use_second_level_cache">false</prop> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.jdbc.batch_size">30</prop> </props> </property> </bean> <!-- Configuring the database --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" primary="true"> <property name="url" value="jdbc:mysql://localhost:3306/sandbox"/> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="username" value="root"/> <property name="password" value=""/> </bean> <!-- Identifying the Hibernate classes --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" primary="true"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties" ref="hibernateProperties"/> <property name="annotatedClasses"> <list> <value>com.tutorial.sandbox.model.Entry</value> </list> </property> </bean> <!-- Wiring in a transaction manager --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- Wiring in the session factory with the open session in view pattern --> <bean name="hibernateInterceptorSessionInView" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- Setting up a bean for easy autowiring, we'll build the class later --> <bean id="entryRepository" class="com.tutorial.sandbox.hibernate.repository.EntryRepository"/> </beans>
So that is enough to get the basic setup in place. Let’s carry on with Part 2 of this tutorial.
2 Responses
Luca
October 17th, 2010 at 9:14 am
1Hi my friend,
I was looking for a simple introduction how to configure a web app in spring with annotation and I found you. Nice tutorial, the pity is that you complicate all with a lot of configuration and using carbon that is not mandatory.
Anyway, thanks, I will find what I’m looking for somewhere else!
Rachel
October 17th, 2010 at 9:40 am
2Hi Luca
I included the configuration for carbonfive as I have found it to be more powerful than the default url mapping and I also included a number of other configuration items that I thought would be useful as a reference.
The reason I wrote this tutorial was because when I was starting with Spring I had looked at a number of simpler tutorials that tended to fall apart as soon as I moved to do something slightly more complicated, so this was intended to setup everything you would need to have to get on to building an average web application.
But I understand your point that it’s not the most simple setup possible. When I get the time I’ll try to put one of those simpler tutorials together and then link it to this one.
RSS feed for comments on this post · TrackBack URI
Leave a reply