【问题标题】:Vaadin and Spring integration - @Inject doesn't workVaadin 和 Spring 集成 - @Inject 不起作用
【发布时间】:2014-02-11 17:51:37
【问题描述】:

我正在尝试将 Vaadin 与 Spring 集成。在我的主要 Vaadin 应用程序类中,我有:

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");

}

}

如果我直接从上下文中获取 bean,我会收到 bean,但是如果我注释从 appContext 接收 bean 的行,那么 @Inject 注释不起作用,我会得到 NullPointerException。我的部署描述符如下:

<?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>

请问有什么建议吗?

【问题讨论】:

    标签: spring integration vaadin inject


    【解决方案1】:

    如果您想将 Vaadin 与 Spring 集成,请使用 @Configurable 功能。然后所有实例化的对象(即使您使用 new MyObject() 代码创建它们)都将与 Spring 上下文集成。您可以在 Spring 文档中找到有关此类设置的更多详细信息:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-using-aspectj

    【讨论】:

      【解决方案2】:

      您可以通过两个简单的步骤将 Vaadin 7 与 Spring 集成:

      1) 创建一个 UIProvider 在 spring 上下文中查找 UIs

      public class SpringUIProvider extends DefaultUIProvider {
      
           @Override
           public UI createInstance(UICreateEvent event) {
               ApplicationContext ctx =  WebApplicationContextUtils
                    .getWebApplicationContext(VaadinServlet.getCurrent().getServletContext());
      
               return ctx.getBean(event.getUIClass());
           }
      }
      

      2) 在 web.xml 中声明UIProvider

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

      并且记得为 UI 类使用 prototype 范围。

      【讨论】:

        猜你喜欢
        • 2012-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多