Vaadin和Spring集成-@Inject不起作用 [英] Vaadin and Spring integration - @Inject doesn't work
问题描述
我正在尝试将Vaadin与Spring集成在一起.在我的主要Vaadin应用程序类中,我有:
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");
}
}
如果我直接从上下文中获取bean,那么我会收到bean,但是如果我注释了从appContext中接收bean的行,那么@Inject注释将不起作用,并且会得到NullPointerException.我的部署描述符如下:
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>
有什么建议吗?
推荐答案
您可以通过两个简单的步骤将Vaadin 7与Spring集成:
You can integrate Vaadin 7 with Spring in two simple steps:
1)创建一个在春季上下文中查找UIs
的UIProvider
:
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)在web.xml中声明UIProvider
:
2) Declare the UIProvider
in web.xml:
<context-param>
<param-name>UIProvider</param-name>
<param-value>org.example.SpringUIProvider</param-value>
</context-param>
并且请记住对UI类使用 prototype 范围.
And remember to use prototype scope for UI classes.
这篇关于Vaadin和Spring集成-@Inject不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!