设置Vaadin和Spring 3-自动布线失败(注释)



我在vaadin应用程序中使用spring3的@Autowire注释时遇到了一些问题。

我做了一个小的测试项目,只使用一个按钮来执行自动连接服务类中的方法。

当我点击按钮时,我会得到一个NullPointerException,因为服务从未被注入。

我已经用@service("calculationService")注释了该服务

这是我的应用程序类:

package vaadinBaas;
// imports
public class MyVaadinApplication extends Application {
private Window window;
private CalculationService calculationService;
@Autowired
public void setCalculationService(CalculationService calculationService) {
    this.calculationService = calculationService;
}
@Override
public void init() {
    window = new Window("Show me the magic");
    setMainWindow(window);
    Button button = new Button("Click Me");
    button.addListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            window.addComponent(new Label(String.valueOf(calculationService.multiply(10, 10))));
        }
    });
    window.addComponent(button);
}
}

这是我的春季应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:ct="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
<ct:component-scan base-package="vaadinBaas" />
<ct:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>

最后是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">
<display-name>Vaadin Web Application</display-name>
<context-param>
    <description>Vaadin production mode</description>
    <param-name>productionMode</param-name>
    <param-value>false</param-value>
</context-param>
<servlet>
    <servlet-name>Vaadin Application Servlet</servlet-name>
    <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
    <init-param>
        <param-name>application</param-name>
        <param-value>vaadinBaas.MyVaadinApplication</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Vaadin Application Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>

我是不是遗漏了一些显而易见的东西?

尝试用组件注释MyVaadinApplication。由于MyVaadinApplication不被视为spring管理类,Autowire不适合您。此外,您还需要使用ClassPathXmlApplicationContext加载bean定义xml,并尝试调用getBean(MyVaadinApplication.class),以便所有自动连接都自动进行

最新更新