Simple problem with Vaadin 6 + Spring 3 + Hibernate

Hi,

I’m a totally beginner with Spring, so excuse me by advance …

I found and follow some tutos on the web and on the vaadin websitee, but unable tomake it working …

I just want a little thing, make vaadin working with spring + hibernate.

My configuration files :

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>HackManagement</display-name>
	<context-param>
		<description>
		Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>false</param-value>
	</context-param>
	<!-- SPRING START -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<!-- SPRING END -->

	<!-- Vaadin servlet -->
	<servlet>
		<servlet-name>HackManagement Application</servlet-name>
		<servlet-class>com.hackmanagement.framework.servlets.AutowiringApplicationServlet</servlet-class>
		<init-param>
			<param-name>application</param-name>
			<param-value>com.hackmanagement.HackManagementApplication</param-value>
		</init-param>
		<init-param>
			<param-name>productionMode</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>HackManagement Application</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<!-- Turn on AspectJ @Configurable support -->
	<context:spring-configured />

	<!-- Activate Spring annotation support -->
	<context:annotation-config />

	<context:component-scan base-package="com.hackmanagement" />

	<!-- Define controller bean for Vaadin application -->
	<bean id="applicationController"
		class="org.springframework.web.servlet.mvc.ServletWrappingController"
		p:servletClass="com.hackmanagement.framework.servlets.AutowiringApplicationServlet">
		<property name="initParameters">
			<props>
				<prop key="application">com.hackmanagement.HackManagementApplication</prop>
				<prop key="productionMode">false</prop>
			</props>
		</property>
	</bean>

	<!-- Turn on @Autowired, @PostConstruct etc support -->
	<bean
		class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	<bean
		class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

	<!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->

	<!-- HSQLDB Data Source -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
		<property name="url"
			value="jdbc:hsqldb:file:data/db;shutdown=true;hsqldb.write_delay=false" />
		<property name="username" value="sa" />
		<property name="password" value="" />
		<property name="initialSize" value="5" />
		<property name="maxActive" value="10" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxOpenPreparedStatements" value="10" />
	</bean>

	<!-- Hibernate Session Factory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>com.hackmanagement.beans.Account</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.connection.autocommit">true</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- ========================= Start of DAO DEFINITIONS ========================= -->

	<!-- proxy for DAO using generic DAO -->
	<bean id="proxyDAO" abstract="true">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- Account Dao definition -->
	<bean id="accountDao" class="com.hackmanagement.services.impl.AccountDaoImpl"
		parent="proxyDAO">
		<constructor-arg value="com.hackmanagement.beans.Account" />
	</bean>

	<!-- ========================= Start of SERVICE DEFINITIONS ========================= -->

	<!-- Transactional proxy for Services -->
	<bean id="proxyService" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED, -java.lang.Exception</prop>
			</props>
		</property>
	</bean>

	<!-- Account service target -->
	<bean id="accountServiceTarget" class="com.hackmanagement.services.impl.AccountDaoImpl"
		init-method="init">
		<property name="accountDao" ref="accountDao" />
	</bean>

	<!-- Account service -->
	<bean id="accountService" parent="proxyService">
		<property name="target" ref="accountServiceTarget" />
	</bean>
</beans>

I use AutowiringApplicationServlet found on this forum !

With that i have my main file.

HackManagementApplication.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import com.hackmanagement.beans.Account;
import com.hackmanagement.services.AccountDao;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

@Configurable(preConstruction = true)
public class HackManagementApplication extends Application {

    private static final long serialVersionUID = 1L;

    @Autowired
    private AccountDao accountDao;

    public AccountDao getAccountDao() {
	return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
	this.accountDao = accountDao;
    }

    @Override
    public void init() {

	Window mainWindow = new Window("HackManagement Application");
	Label label = new Label("Hello Vaadin user");
	mainWindow.addComponent(label);
	setMainWindow(mainWindow);

	Account acc = new Account();
	acc.setLogin("toto");
	acc.setEmail("toto@gmail.com");
	acc.setPassword("s3Cr3T");
	accountDao.create(acc);

	MainFrame f = new MainFrame("Gestion des comptes");

	System.out.println("------- LIST");

	for (Account a : accountDao.readAll())
	    a.print();
    }
}

And my problem is that Spring don’t load my AccountDAOautomaticaly !

So i have a NullPointerException when i do “accountDao.create(acc);”

I’m sure you can help me, so thanks by advance !

I don’t think you can just use the @Autowire annotation in the application class as it’s not processed by Spring anyway and the annotation is not handled.

Instead, you should use the Spring’s ApplicationContext’s getBean method.

You can get the ApplicationContext by using the WebApplicationContextUtils.getRequiredWebApplicationContext method for what the ServletContext parameter is fetched from Vaadin application for example like this:


ServletContext context = ((WebApplicationContext) getContext()).getHttpSession().getServletContext();

ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

then from appContext call


appContext.getBean("accountDao");

in your applicationContext.xml you should also specify the scope attribute of your bean to ‘prototype’. Specify that right after you bean id.

After that inside the beans you can autowire other beans as their initialization is now handled by the ApplicationContext.

Hope this helps.

i am tr’ing for vaadin-spring-hibernate sample , if the above code works ,please tell me how to configure vaadin-spring-hibernate

Thanks for help