Vaadin and Spring integration - @Inject doesn't work

我们两清 提交于 2019-12-07 00:20:30

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.

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

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