Vaadin and Spring integration - @Inject doesn't work

混江龙づ霸主 提交于 2019-12-08 08:43:00

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21269939/vaadin-and-spring-integration-inject-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!