Spring integration helper class

It would be nice to have a short guide on how put Spring + Vaadin + Spring Stuff Add-on all together in a new project, just to start from scratch in the right direction.

Hello !
I am currently trying to use Vaadn with a Spring MVC project and Hibernate.
I was able to reproduce the “Hello World” example.
Now, I would like to use my own beans. That’s why I would like to use the component scanner in ApplicationContext.xml instead of declaring every single bean. When I do this, I am getting a lot of different NoSuchBeanDefinition and BeanCreation Exception. I dont have them without Vaadin.
Here is my web.xml and my ApplicationContext.xml.

I guess that my problem isn’t directly linked with Vaadin, but has more something to do with spring. Indeed, before integrating Vaadin, i had the component scanner in my servlet.

    
<context:annotation-config />
<context:component-scan base-package="net.sebpx.subgeste" />

When I am moving it into my ApplicationContext.xml, it is no longer working ! Obviously, my beans are not detected.

Every help / advice would be very welcome !

Thank you in advance

Sebastian

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">

    <display-name>Vaadin+Spring Demo #2</display-name>
    <description>Vaadin+Spring Demo #2</description>

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

    <!-- Vaadin servlet -->
    <servlet>
        <servlet-name>AutowiringApplicationServlet</servlet-name>
        <servlet-class>net.sebpx.subgeste.vaadin.util.AutowiringApplicationServlet</servlet-class>
        <init-param>
            <param-name>application</param-name>
            <param-value>net.sebpx.subgeste.vaadin.HelloWorld</param-value>
        </init-param>
        <init-param>
            <param-name>productionMode</param-name>
            <param-value>false</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>AutowiringApplicationServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app> 

ApplicationContext.xml


<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- $Id$ -->
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="
   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">

    <!-- Activate Spring annotation support -->
    <context:annotation-config/>
     <context:component-scan base-package="net.sebpx.subgeste" /> 
 <!--  <bean class="net.sebpx.subgeste.vaadin.MyBean" p:name="foobar"/> -->
</beans>

HelloWorld Application

package net.sebpx.subgeste.vaadin;



import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

import net.sebpx.subgeste.service.HospitalServiceImpl;

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

public class HelloWorld extends Application {

    @Autowired
    private HospitalServiceImpl hsi;

    @Override
    public void init() {
        Window mainWindow = new Window("Vaadin+Spring Demo");
    //    mainWindow.addComponent(new Label("myBean info: " + this.myBean.info()));
        mainWindow.addComponent(new Label(hsi.listHospitals().toString()));
       setMainWindow(mainWindow);
    }
}

And the errors :confused:


....
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory net.sebpx.subgeste.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory]
 found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 49 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory]
 found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
	... 51 more

Hi,

I’m trying to get rid of the deprecated ContextApplicationFactoryBean in my vaadin-context file and instead specify my own SpringContextApplication-class as the bean-class and use the factory-method ‘get’ as described in the javadoc:s. My problem is that other vaadin-components annotated with @Component is no longer able to @Autowire in my SpringContextApplication-class, instead I get a NoSuchBeanDefinitionException.

Any suggestions are appreciated!

br Svante

edit: Running version 1.0.284
edit: Heck, overriding the static method (‘get’) doesn’t work to well, does it… rename it to myFactoryMethod did the trick.

Hi, I created a maven project by adding your source code and configuration. It works…only probem is the “myApplicationBean: MyApplicationBean@58cca9” changes everytime I refresh the browser, which according to your specifications it should not. Where do you think I made a mistake? Thanks in advance. I posted my pom file here:

http://code.google.com/p/dellroad-stuff/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=6

signed!

Hello,

I have just started a new project using Spring and Vaadin. I explain you my problem :

I have 3 bundles : Model - Services - Store and Web

The bundle Model, Service and Store are Spring archetypes and the bundle Web is a Vaadin archetype. So what I want to do is the integration of Spring with Vaadin. I would like loading all applicationContext.xml through my bundle Web thanks to the web.xml.
I don’t want to use the solution with Aspect J and Spring MVC (the DispatcherSerlvet) so I try setting up the demo2.zip (of Archie Cobbs) but it doesn’t work…

I get the error below :

Thanks for any help or suggestion !!

My apologies to everyone in that I don’t have time to create any more elaborate documentation. Mostly what the
Spring Stuff
add-on does is tie together two existing things that are themselves very well documented: Spring and Vaadin. Once you understand the relevant parts of how those two things work, then this add-on will make sense.

The best thing you can do to learn about this add-on specifically is to inspect
the example application
.

Cheers,
-Archie

You may have hit
issue #7
. If so, try upgrading to version 1.0.349.

There was a bug in the demo. I didn’t realize that a browser refresh causes a window close event, and was closing the application on window close events. Should be fixed now.

FYI,

I just tested dellroad-stuff with Vaadin 7.0.0.alpha2 and it seems to work. All you have to do is configure both the
application
and the
root
in web.xml:

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
        <init-param>
            <param-name>application</param-name>
            <param-value>com.example.HelloWorld</param-value>
        </init-param>
        <init-param>
            <param-name>root</param-name>
            <param-value>com.example.HelloRoot</param-value>
        </init-param>
    </servlet>

Check out the
vaadin7 branch on github
for an example.

Hey, Archie

I would like to thank you for your contribution. It’s absolutely fantastic!

I would like to suggest a small fix for a bug that I have found. I filled issue report in google code, but I copy it here so maybe other people see it and can suggest solution (I am not sure if everything is ok with my code):

NotSerializableException thrown when serializing subclass of ContextApplication

When serializing subclass of ContextApplication (for example, when applicaton server like Tomcat or Websphere Community Edition is stopped) an Exception of type NotSerializableException is being thrown because ContextApplication has executorService field. This field contains object of type FinalizableDelegatedExecutorService, which is not serializable.

Suggested fix: mark field executorService as transient. Add writeObject() and readObject() methods that deal with executorService field.

Thanks for filing
issue #9
which is now fixed and will be included in the next release.

Also: Regarding Vaadin 7 support, there are a few things that are needed and/or desired to change. So there is now a new package

org.dellroad.stuff.vaadin7

which contains Vaadin 7 versions of a couple of classes.

Once Vaadin 7 becomes mainstream, these will be merged back into the

org.dellroad.stuff.vaadin

package.

Have you had any problems with deserialization of sessions when using @Configurable? Please check my
stackoverflow question

Hi,

I’m not able to get your example running.

I’m using Vaadin7-alpha3 an Spring3.1 and I cannot get rid of this error:

11.07.2012 09:46:50 org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet [hello]
 in context with path 
[/vaadin7] threw exception [java.lang.NullPointerException]
 with root cause
java.lang.NullPointerException
	at com.example.HelloRoot.init(HelloRoot.java:69)
	at com.vaadin.ui.Root.doInit(Root.java:903)
	at com.vaadin.Application.getRootForRequest(Application.java:2262)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleBrowserDetailsRequest(AbstractCommunicationManager.java:2328)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:466)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:350)

Any suggestions?

Thx in advance
Thomas

Seems like your classes has not been weaved and as a result auto-wiring does not happen.

I had the same Exception after converting project to maven.

If you have made maven project to play with an example you may need to add

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <configuration>
    <aspectLibraries>
      <aspectLibrary>
	<groupId>org.dellroad</groupId>
        <artifactId>dellroad-stuff-vaadin</artifactId>
      </aspectLibrary>
      <aspectLibrary>
 	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
      </aspectLibrary>
    </aspectLibraries>
  </configuration>
  <executions>
    <execution>
      <goals>
	<goal>compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

in your pom’s <build><plugins>...</plugins></build>

Don’t forget also

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${org.springframework-version}</version>
  <scope>provided</scope> 
</dependency>

-alex

Hi Alex,

thank you for answer, but so far I don’t have set a maven project for this example. Do you mind sending your pom to me or post it here?

Thx
Thomas

sure, here it is (see attachments)

just put it in the root of the
https://github.com/archiecobbs/dellroad-stuff-vaadin-spring-demo3.git
clone.
Don’t forget to checkout vaadin7 branch

then mvn jetty:run should start this demo at http://localhost:8080/vaadin-spring

It uses 7.0.0.alpha2. For alpha3 you need to change sources because of

[quote]
Label.ContentMode is no longer an inner class and has moved to a different package. Update your imports to use com.vaadin.terminal.gwt.client.ui.label.ContentMode
[/quote] (see
Vaadin7/MigrationGuide
)

hope it helps.

-alex
12443.xml (5.29 KB)

I’m trying to use this Spring Stuff add-on, but the AspectJ weaver throws this exception:

java.lang.RuntimeException: Cannot register non aspect: org$dellroad$stuff$spring$PostCompletionAspect , org.dellroad.stuff.spring.PostCompletionAspect

I’ve looked in the add-on’s jar file and indeed this PostCompletionAspect is not there, yet it is specified in the META-INF/aop.xml file in that same jar and the maven pom.xml file for the add-on does not specify a dependency to another jar that contains it.
What do I need to do to make the add-on work with AspectJ ?

I just upgraded from 1.0.189 and got the same problem. I hope this will be solved soon.

Thanks Archie, that’s great stuff!

I used it in the context of Spring DM and it works well.

There is only little to add to make it running with Spring DM. Here the web.xml for those dealing with this situation. Don’t forget to start both the spring-osgi-extender and the spring-osgi-web-extender as bundles.


WEB-INF/web.xml
:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>My Application</display-name>

	<!-- The regular spring dm configuration files -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:META-INF/spring/module-context.xml
			classpath:META-INF/spring/module-osgi-context.xml
		</param-value>
	</context-param>

	<!-- This combines the Web MVC application context with the OSGi application context -->
	<context-param>
		<param-name>contextClass</param-name>
		<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
	</context-param>

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>spring</servlet-name>
		<!-- The class that extends the AutowiringApplicationServlet (see Archie's code) -->
		<servlet-class>de.however.main.MyApplicationServlet</servlet-class>
		<init-param>
			<param-name>contextClass</param-name>
			<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
		</init-param>
		<init-param>
			<description>Vaadin production mode</description>
			<param-name>productionMode</param-name>
			<param-value>false</param-value>
		</init-param>
		<init-param>
			<description>Vaadin application</description>
			<param-name>application</param-name>
			<param-value>de.however.main.Main</param-value>
		</init-param>
		<init-param>
			<description>Application widgetset</description>
			<param-name>widgetset</param-name>
			<param-value>de.however.widgetset.MyWidgetSet</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	
	<servlet-mapping>
        	<servlet-name>spring</servlet-name>
        	<url-pattern>/*</url-pattern>
	</servlet-mapping>

	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>

</web-app>

The
META-INF/spring/module-context.xml
defines common spring stuff:


[...]

	<context:component-scan base-package="de.however" />
[...]

The
META-INF/spring/module-osgi-context.xml
can define osgi service beans (as usual).

Eugen