Question

I'm trying to integrate Vaadin with Spring. In my main Vaadin application class I have:

public class MyVaadinApplication extends UI {

@Inject
private PrivatePersonBo privatePersonBo;

@Override
public void init(VaadinRequest request) {
    Layout layout = new FormLayout();
    layout.setCaption("New Private Person");
    setContent(layout);

    ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/spring/Context.xml");
    appContext.getBean(MyVaadinApplication.class);
    PrivatePersonBo privatePersonBo = (PrivatePersonBo) appContext.getBean("privatePersonBo");
    PrivatePerson existingEmployee = privatePersonBo.findByName("TestUserName");

}

}

If I get bean from context directly I recieve bean, however If I comment line which recieves bean from appContext then @Inject annotation doesn't work and I get NullPointerException. My deployment descriptor is below:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
     version="3.0">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:resources/spring/Context.xml</param-value>
</context-param>

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

<servlet>
    <servlet-name>VaadinApplicationServlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
        <param-name>UI</param-name>
        <param-value>pl.adamsalata.MyVaadinApplication</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>VaadinApplicationServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Any suggestions please?

Was it helpful?

Solution

You can integrate Vaadin 7 with Spring in two simple steps:

1) Create a UIProvider that lookup UIs in spring context:

public class SpringUIProvider extends DefaultUIProvider {

     @Override
     public UI createInstance(UICreateEvent event) {
         ApplicationContext ctx =  WebApplicationContextUtils
              .getWebApplicationContext(VaadinServlet.getCurrent().getServletContext());

         return ctx.getBean(event.getUIClass());
     }
}

2) Declare the UIProvider in web.xml:

<context-param>
                <param-name>UIProvider</param-name>
                <param-value>org.example.SpringUIProvider</param-value>
</context-param>

And remember to use prototype scope for UI classes.

OTHER TIPS

If you want to integrate Vaadin with Spring then use @Configurable feature. Then all instantiated objects (even if you create them using new MyObject() code) will be integrated with Spring context. You can find more details about such setup in Spring documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-using-aspectj

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top