设置Vaadin和Spring 3 - 自动装配失败(注释)

时间:2012-03-14 15:52:07

标签: spring vaadin

我在vaadin应用程序中使用spring 3的@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>

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

尝试使用Component注释注释MyVaadinApplication。由于MyVaadinApplication不被视为弹簧管理类,因此Autowire不适合您。您还需要使用ClassPathXmlApplicationContext加载bean定义xml并尝试调用getBean(MyVaadinApplication.class)以便所有自动装配自动发生