2014-01-21 84 views
1

我正在嘗試將Vaadin和Spring集成。在我的主要Vaadin應用I類有:Vaadin和Spring集成 - @Inject不起作用

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

} 

} 

如果我從上下文豆直接我收到豆,但是如果我評論線從appContext臨危豆然後@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> 

任何建議嗎?

回答

1

可以在兩個簡單的步驟與Spring集成Vaadin 7:

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.xmlUIProvider

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

並記得使用原型範圍爲UI類。

+0

謝謝,它的工作原理。 – BlueLettuce16