Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Problem with Spring integration

Klaus Julin
1 decade ago Jul 14, 2010 6:46pm
Henri Sara
1 decade ago Jul 15, 2010 6:21am
Klaus Julin
1 decade ago Jul 15, 2010 8:21am
Henri Sara
1 decade ago Jul 15, 2010 9:03am
Martin W
1 decade ago Jan 17, 2011 9:11pm
Deleted User
1 decade ago Jan 17, 2011 10:09pm

Klaus Julin: Hi!

I'm trying to get a simple vaadin project working with spring as explained in spring wiki.

My context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- Data classes -->
  <bean id="userDao"
    class="com.collapsux.filer.datalevel.impl.UserDaoImpl">
  </bean>

  <bean id="logDao"
    class="com.collapsux.filer.datalevel.impl.FilerLogDaoImpl">
  </bean>
  

  <!-- Service components -->
  <bean id="filerService"
    class="com.collapsux.filer.services.impl.FilerServiceImpl">
    <property name="userDao">
      <ref bean="userDao" />
    </property>
    <property name="logDao">
      <ref bean="logDao" />
    </property>
  </bean>

  <!-- Database connection -->
  <bean id="datasource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
      value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:filer" />
    <property name="username" value="sa" />
    <property name="password" value="" />
  </bean>

  <!-- JPA-definitions -->

  <!-- Factory for entitymanagers -->
  <bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="jpaVendorAdapter">
      <bean
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
  </bean>

  <!-- Notice @PersistenceContext-annotations -->
  <context:annotation-config />

  <!-- Transaction management -->
  <bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
      ref="emf" />
  </bean>

  <!-- Notice @Transactional annotations -->
  <tx:annotation-driven />
  
  <!-- Turn on AspectJ @Configurable support --> 
<context:spring-configured />

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

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

</beans>

and my web.xml contains this:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:context.xml</param-value>
  </context-param>

My main class starts:

@Configurable(preConstruction = true)
public class FilerApplication extends Application
{
	
	private static final long serialVersionUID = 6777561160414111445L;

	public static final String VERSION = "1.0";
	
	@Autowired
	private FilerService service;
	private User currentUser;
	
	private Window mainWindow;
	private Panel mainPanel;

    @Override
    public void init()
    {
  	
    	mainWindow = new Window("Filer " + VERSION);	
    	
		try {
			service.importDataToDatabase("C:/dev/workspace/filer-services/database_export.xml");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

but when I run mvn:tomcat run, it gives me this:

C:\dev\workspace\filer-ui-vaadin>mvn tomcat:run
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Vaadin Web Application
[INFO]    task-segment: [tomcat:run]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing tomcat:run
[INFO] [aspectj:compile {execution: default}]
[WARNING] bad version number found in C:\Documents and Settings\kj\.m2\repositor
y\org\aspectj\aspectjrt\1.6.1\aspectjrt-1.6.1.jar expected 1.6.3 found 1.6.1
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\dev\workspace\filer-ui-vaadin\src\
main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [tomcat:run {execution: default-cli}]
[INFO] Running war on http://localhost:8080/filer-ui-vaadin
[INFO] Using existing Tomcat server configuration at C:\dev\workspace\filer-ui-v
aadin\target\tomcat
14.7.2010 21:34:43 org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
14.7.2010 21:34:43 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.26
14.7.2010 21:34:44 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
- Root WebApplicationContext: initialization started
- Refreshing org.springframework.web.context.support.XmlWebApplicationContext@2c
5444: display name [Root WebApplicationContext]; startup date [Wed Jul 14 21:34:
44 EEST 2010]; root of context hierarchy
- Loaded 0 bean definitions from location pattern [classpath*:context.xml]
- Bean factory for application context [org.springframework.web.context.support.
XmlWebApplicationContext@2c5444]: org.springframework.beans.factory.support.Defa
ultListableBeanFactory@1e45e3
- 0 beans defined in org.springframework.web.context.support.XmlWebApplicationCo
ntext@2c5444: display name [Root WebApplicationContext]; startup date [Wed Jul 1
4 21:34:44 EEST 2010]; root of context hierarchy
- Unable to locate MessageSource with name 'messageSource': using default [org.s
pringframework.context.support.DelegatingMessageSource@13c1b69]
- Unable to locate ApplicationEventMulticaster with name 'applicationEventMultic
aster': using default [org.springframework.context.event.SimpleApplicationEventM
ulticaster@1263b07]
- Unable to locate ThemeSource with name 'themeSource': using default [org.sprin
gframework.ui.context.support.ResourceBundleThemeSource@cf7fda]
- Pre-instantiating singletons in org.springframework.beans.factory.support.Defa
ultListableBeanFactory@1e45e3: defining beans []; root of factory hierarchy
- Published root WebApplicationContext as ServletContext attribute with name [or
g.springframework.web.context.WebApplicationContext.ROOT]
- Root WebApplicationContext: initialization completed in 172 ms
14.7.2010 21:34:44 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
14.7.2010 21:34:44 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

And when I start the application in browser it throws a null pointer exception from the FilerApplication class from that row where it first time uses the filerService.

It seems to me that my injection does not work so can someone tell me what I'm missing or have configured wrong??

Thanks in advance!!

I'm no expert in spring, but I work so
class spring vaadin

mport java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import  com.vaadin.*;
import com.vaadin.terminal.gwt.server.WebApplicationContext;

@SuppressWarnings("serial")
public class SpringControlador implements Serializable {
	
    

	
	private ApplicationContext context;

    public SpringControlador(Application  application) {
        ServletContext servletContext = 
                ((WebApplicationContext) application.getContext())
                .getHttpSession().getServletContext();
        context = WebApplicationContextUtils.
                getRequiredWebApplicationContext(servletContext);
    }

    public Object getBean(final String beanRef) {
        return context.getBean(beanRef);
    }

	 
}

my application get bean spring

ctx = new SpringControlador(this);
		controlDominio = (ControladorDominio) ctx.getBean("controladorDominio");
		controlDispositivo = (ControladorDispositivo) ctx
				.getBean("controladorDispositivo");

class control

public class ControladorDominio {

	
	private ControladorInventario controladorInventario;
	
	public ControladorInventario getControladorInventario() {
		return controladorInventario;
	}
       .....................
	

my control

..........

public class ControladorInventario  {

        private ServicioRegion servicioRegion;
	
	public ControladorInventario(){
		
	}
	
	
	public void setServicioRegion(ServicioRegion servicioRegion) {
		this.servicioRegion = servicioRegion;
	}

	public ServicioRegion getServicioRegion() {
		return servicioRegion;
	}

..................

my service

public class ServicioRegion implements Serializable{

	
	private RegionDAO regionDAO;
	
	public RegionDAO getRegionDAO() {
		return regionDAO;
	}
	
	public void setRegionDAO(RegionDAO regionDAO) {
		this.regionDAO = regionDAO;
	}

	//all operation my classs
	public List<Region> getListaRegion() throws ExcepcionDao {
		return regionDAO.findAll();
	}
...........

my DAO

SqlMapClientTemplate templateSpring = this.getSqlMapClientTemplate();
	public static Log logger = LogFactory.getFactory().getInstance(
			RegionDAO.class);
	
	@Override
	public int insert(Region item) throws ExcepcionDao {
		// TODO Auto-generated method stub
		try {
			Integer key = (Integer) templateSpring.insert("insertRegion", item);
			return key;
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			throw new ExcepcionDao(e.getMessage());

		}
	}
....
ALL OPERATION 

MY MAP

<sqlMap namespace="Region">

	<typeAlias type="com.gendarmeria.inventarioinformatico.dominio.Region"
		alias="region" />
	<resultMap id="resultRegion" class="region">
		<result property="idRegion" column="id" />
		<result property="nombre" column="nombre" />
		<result property="sigla" column="sigla" />
	</resultMap>
	<insert id="insertRegion" parameterClass="region">
		<![CDATA[ 
  			INSERT 
  				INTO region(nombre,sigla) 
  			VALUES
  				(#nombre#,#sigla#)
		]]>
		<selectKey resultClass="int">
		 <![CDATA[ 
  		 	SELECT LAST_INSERT_ID()
		]]>
		</selectKey>
	</insert>
ALL OPERATION SQL HERE
............

MY 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>InventarioInformaticoGendarmeria</display-name>
	<context-param>
		<description>
		Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>false</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext*.xml</param-value>
	</context-param>
	<servlet>
all parmam vaadin HERE
.................

MY CONTEXT SPRING

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

<!-- Beans_______________________________________________ -->

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

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:jdbc.properties" />
	</bean>

	<!-- data Source_____________________________________________________________________ -->


	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${xxxxxxxxxxxx}" />
		<property name="password" value="${xxxxxxxxxxxxxx}" />
	</bean>

	<!-- Transaccion Manager______________________________________________________________ -->

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>


	<tx:annotation-driven transaction-manager="transactionManager" />
	<context:annotation-config />

     dependency injection HERE

<bean id="servicioRegion"
		class="XXXXXXXXXXXXXXXXXXXXX">
		<property name="regionDAO">
			<ref bean="beanRegionDAO" />
		</property>
	</bean>
...........................................

   

work fine

Last updated on Jan, 17th 2011
Roberto Neto
1 decade ago Aug 16, 2012 2:50pm
Filip Kaliński
1 decade ago Sep 11, 2012 7:55pm