Spring integration helper class

Looks like that auto-deploy stuff is not compatible with
ContextApplication
and deserialization. In a nutshell, the problem is that it’s trying to initialize the Spring application context outside of any Vaadin HTTP request thread. Because there’s no Vaadin HTTP request, there’s no way to know which Vaadin
Application
we’re talking about. Obviously you need to know that to initialize the associated Vaadin Spring application context.

This might actually work someday in Vaadin 7.x if they ever fix
Issue #9953
.

Hi,
thanks for your answer.

I’m afraid we can’t switch to Vaadin 7 for the time being :frowning: Is there any way to fix this? I realized that my auto-deploy method dosn’t do anything fancy - it simply redeploys the application and restarts the server, so in production the error will occur as well, won’t it? As soon, as the server has to be restarted, and there are any applications serialized into session, all of them will be invalidated, since they won’t be able to instantiate their “private” Spring context.

…or am I wrong? :slight_smile:

I’m not sure whether or how it can work. It may be that in production you will not see this problem… why go ahead and try it?

I don’t serialize any Vaadin sessions in my normal work so it’s not something I’m familiar with.

If somehow you can get the deserialization to happen within an invocation of
ContextApplication.invoke()
, then that will fix the problem. Not sure how to do that though with your auto-deploy stuff.

Hi,
I’m having trouble wiring spring resources in my application using the springstuff addon. I used a existing backend and I’m building the frontend with Vaadin7. (Spring 3.1.1)
When I define my service like this:


@Resource(name="toestelOpstellingService")
private IToestelOpstellingService toestelOpstellingService;

My service is null.

When using


ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/metadblucht.spring.xml");
toestelOpstellingService = (IToestelOpstellingService) applicationContext.getBean("toestelOpstellingService")

its works but I want to use the annotations.

The services are annotated with @Service(“serviceName”)

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" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
 
	
	
	<!--  Spring Context -->
	<listener> 
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>  
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/metadblucht.xml</param-value>
	</context-param>
	
<!-- 	 Spring Security -->
<!-- 	<filter> -->
<!--         <filter-name>springSecurityFilterChain</filter-name> -->
<!--         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -->
<!--     </filter> -->
<!--     <filter-mapping> -->
<!--         <filter-name>springSecurityFilterChain</filter-name> -->
<!--         <url-pattern>/*</url-pattern> -->
<!--     </filter-mapping> -->
    
    <!-- Vaadin Servlet -->
    <servlet>
	  	<servlet-name>metadblucht</servlet-name>
	  	<servlet-class>org.dellroad.stuff.vaadin7.SpringVaadinServlet</servlet-class>
	  	<init-param>
	  		<description>Vaadin UI to display</description>
			<param-name>UI</param-name>
	  		<param-value>be.vmm.lucht.gui.MetadbluchtUI</param-value>
	  	</init-param>
  </servlet>
	  <servlet-mapping>
	  	<servlet-name>metadblucht</servlet-name>
	  	<url-pattern>/*</url-pattern>
	  </servlet-mapping>
	  <servlet-mapping>
	    <servlet-name>metadblucht</servlet-name>
	    <url-pattern>/VAADIN/*</url-pattern>
	</servlet-mapping>
    
    
    
   <!-- Spring Dispatcher Servlet -->
<!--     <servlet> -->
<!--         <servlet-name>dispatcher</servlet-name> -->
<!--         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> -->
<!--         <init-param> -->
<!--         	<param-name>contextConfigLocation</param-name> -->
<!--         	<param-value>classpath:metadblucht-web.spring.xml</param-value> -->
<!--         </init-param> -->
<!--         <load-on-startup>2</load-on-startup> -->
<!--     </servlet> -->
<!--      <servlet-mapping> -->
<!--     	<servlet-name>dispatcher</servlet-name> -->
<!--     	<url-pattern>/servlet/*</url-pattern> -->
<!--     </servlet-mapping> -->
    
    
    
   <resource-ref>
    <description>Database Connection Pool</description>
    <res-ref-name>jdbc/metadblucht</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <resource-ref>
    <description>Database Connection Pool</description>
    <res-ref-name>jdbc/security</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <resource-ref>
    <description>Database Connection Pool</description>
    <res-ref-name>jdbc/config</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  
  <display-name>Metadblucht-Vaadin</display-name>
  <context-param>
  	<description>Vaadin production mode</description>
  	<param-name>productionMode</param-name>
  	<param-value>false</param-value>
  </context-param> 
     
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

metadblucht.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
       
	<context:annotation-config />	
	<context:component-scan base-package="be.vmm" />
	
	<!-- Our application inherits transactions, and auditing from the VMM corelib configuration -->
	<import resource="classpath:core-auditing.spring.xml"/>
	<import resource="classpath:core-transaction.spring.xml"/>
	<import resource="classpath:core-config.spring.xml"/>
	
	<import resource="dao.spring.xml" />
	<import resource="metadblucht-auditing.spring.xml" />
	<import resource="metadblucht-security.spring.xml" />
	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 
	
	
</beans>

Some thoughts…

Try adding
@Required
to your
@Resource
, or switch to
@Autowired
so it can’t be left null without an error.

You shouldn’t manually add those two
BeanPostProcessors
because
context:annotation-config/
does that for you.

It looks like you are using
metadblucht.xml
as both the servlet-scope application context and the
VaadinSession
-scope application context. This is wrong. You should define two separate application contexts, and the latter should have its location defined using the
configLocation
servlet
init-param
.

See the demo web application for an example.

Ty for your reply.

I changed the web.xml to


<!--  Spring Context -->
	<listener> 
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>  
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:metadblucht.xml</param-value>
	</context-param>
    
	<!--     Vaadin Servlet -->
    <servlet>
	  	<servlet-name>MetadbluchtVaadin</servlet-name>
	  	<servlet-class>org.dellroad.stuff.vaadin7.SpringVaadinServlet</servlet-class>
	  	<init-param>
			<param-name>UI</param-name>
	  		<param-value>be.vmm.lucht.gui.MetadbluchtUI</param-value>
	  	</init-param>
	  	<init-param>
	  		<param-name>configLocation</param-name>
			<param-value>classpath:MetadbluchtVaadin.xml</param-value>
	  	</init-param>
	  	 <init-param>
            <param-name>productionMode</param-name>
            <param-value>false</param-value>
        </init-param>
  	</servlet>
	  <servlet-mapping>
	  	<servlet-name>MetadbluchtVaadin</servlet-name>
	  	<url-pattern>/*</url-pattern>
	  </servlet-mapping>   

metadblucht-vaadin.xml contains only context:annotation-config/
metadblucht.xml same content as in previous post except for the two beanPostProcessors

VaadinServlet.getCurrent().getServletContext() return metadblucht.xml ← shouldnt this be metadblucht-vaadin.xml?

I deployed demo3 (eclipse 4.2, spring 3.1.1, tomcat 7.0.39) and I have the same problem
SEVERE:
java.lang.NullPointerException
at com.example.HelloUI.init(HelloUI.java:75)

Not sure what you mean.
getServletContext()
returns a
ServletContext
object, not an application context.

In any case, it is correct that the
metadblucht.xml
application context is associated with the servlet context, while the
MetaBluchtVaadin.xml
application context is associated with the
VaadinSession
.

I just tried building and running the dellroad-stuff-vaadin-spring-demo3 using Spring 3.1.x and it worked fine.

Try building it using the supplied ant
build.xml
file. I bet Eclipse is not running the AJC compiler.

Hi Archie,
I made the changes but got the same result, still NPE

I am attaching (eclipse export) a stripped version of the application if you could take a look at it.
13009.zip (109 KB)

If I had more time I would be happy to help you debug further. Unfortunately I don’t, sorry.

My only suggestion is to try to figure out how your app and the demo app are different.

Hi,

Could you post an example project for Vaadin 7, too.

It’s already there as part of the same github project. Just switch to the
vaadin7
branch.

Hi, Archie.

Is there any particular reason why org.dellroad.stuff.vaadin7.VaadinApplication is not implementing Serializable?

No. I don’t use serialization myself so the need hasn’t come up. It should be easy to add.

Do you happen to have a patch handy that you have tested out?

-Archie

Archie, not currently, but I will work on one this week. It is not a big issue, I just don’t want my Tomcat production instance administrator to be scared by some random exceptions :slight_smile:

Hi Archie, I used your great addon successfully with Vaadin 6. But now I have to move to Vaadin 7. Is is supposed to work with 7.1.3? I cannot get it to work. I tried your sample application on github but that gives me an NPE in HelloUI when trying to access the HelloWorld bean, which should have been autowired at this point.
And by the way, how could I use the whole thing without autowiring? I prefer using the traditional approach via XML only and I have no idea how the UI class could get it’s properties then.
Best Regards
Ralph

It should work find with Vaadin 7.1.3. I just updated the demo to pull that version. When I run the demo locally it works fine.

Try updating from github and see if that helps.

Sure, autowiring is not required. Just define whatever beans you want in the Vaadin application’s XML file (in the demo, that’s the
HelloWorld.xml
file).

-Archie

Thanks Archie, using your git demo from scratch using pure ant/ivy worked now on 7.1.3 as well.
Must have been a problem with my manual eclipse setup.
Nevermind me asking :slight_smile:

Hi Alexander,
don’t know if you still have problems to get it to run in eclipse.
I had similiar problems. After adding the dellroad-stuff jar to the aspectj Aspect Path (AspectJ tools->Configure AspectJ Build Path), it worked.

Hi everyone,

It’s my first time on this forum.

Did you think about using
UIProvider
mechanism to add spring autowire capabilities?
You can have a look at a really simple example that uses this solution on github:
spring-vaadin-example

Yes, that’s very simple. But you don’t get per-session application contexts… only the servlet-context scoped context.