2
我在vaadin應用程序中使用spring 3的@Autowire註釋遇到了一些麻煩。設置Vaadin和Spring 3 - 自動裝配失敗(註釋)
我做了一個很小的測試項目,只有一個按鈕,它在自動裝配的服務類中執行一個方法。
當我單擊按鈕時,我收到一個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);
}
}
這是我的Spring應用程序上下文:
<?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>
我在這裏錯過了一些明顯的東西嗎?
是** CalculationService **在同一個包中** vaadinBaas **? – ManuPK 2012-03-14 16:38:51
它在一個分包「服務」。 – geoffreydv 2012-03-14 16:45:17