我正在尝试将Spring Dependency Injection与我的Vaadin application/servlet
集成。但是依赖注入不起作用。我认为Spring可以很好地读取组件:
2013-01-15 06:57:11,361 [localhost-startStop-1] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [com/nortal/pirs/spring/application-context.xml]
2013-01-15 06:57:12,012 [localhost-startStop-1] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fa5214b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.config.internalBeanConfigurerAspect,mainController,UserManager,VisitManager,mainWindow,org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
因为我可以看到我所需的对象,例如UserManager
,mainWindow
...
然而,当涉及实际注射时,它似乎不起作用。我得到了nullpointerexceptions。
现在我正试图用MainWindow
来测试它,这里应该注入MainWindow
的应用程序:
@Configurable
public class PirsVaadinController extends Application implements
NavigatorInterface {
private String title = "WELCOME";
private transient Logger log;
private Map userData;
private boolean useFakeData = false;
@Autowired
private MainWindow mainWindow;
public UserManagerInterface getUserManager() {
return userManager;
}
@Autowired
public void setUserManager(UserManagerInterface userManager) {
this.userManager = userManager;
}
是的我知道我把@Autowired
放在了两者上:setter和变量本身,但它无论如何都不起作用。
主窗口:
@org.springframework.stereotype.Component
@org.springframework.context.annotation.Scope("session")
public class MainWindow extends Window {
我的web.xml:
<!-- Vaadin -->
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>PirsVaadinFrontend</servlet-name>
<servlet-class>
com.vaadin.terminal.gwt.server.ApplicationServlet
</servlet-class>
<init-param>
<description>Vaadin application class to start</description>
<param-name>application</param-name>
<param-value>
com.nortal.pirs.presentation.vaadin.PirsVaadinController
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>PirsVaadinFrontend</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com\nortal\pirs\spring\application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- EmailCheck REST web service -->
<servlet>
<servlet-name>emailCheckService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.nortal.pirs.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>emailCheckService</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
<!-- General parameters -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
My 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="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/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:component-scan base-package="com.nortal.pirs.test.independent" />
<context:component-scan base-package="com.nortal.pirs.businesslogic.logic" />
<context:component-scan base-package="com.nortal.pirs.presentation.vaadin" />
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
</beans>
我的Maven pom.xml:
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>EBR Spring Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/release</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>EBR External Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/</sourceDirectory>
<directory>${basedir}/build</directory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Additional -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<versionRange>1.2</versionRange>
<goals>
<goal>test-compile</goal>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<!-- Vaadin - Spring -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<!-- Required as the plugin does not resolve this by default -->
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我没有提供依赖项,因为我可能达到了字符限制,但依赖项似乎并不缺乏imho。在pom.xml中我收到此错误:
Multiple annotations found at this line:
- Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.2:test-compile (execution: default, phase:
process-test-sources)
- Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.2:compile (execution: default, phase: process-
sources)
但是从命令行看,一切似乎都在编译好:
mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building PIRS AMS 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- aspectj-maven-plugin:1.2:compile (default) @ PIRS_AMS_WEB ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ PIRS_AMS_W
EB ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Dropbox\EclipseWorkspace\PIRS_FIX\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.1:compile (default-compile) @ PIRS_AMS_WEB --
-
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to D:\Dropbox\EclipseWorkspace\PIRS_FIX\build\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.617s
[INFO] Finished at: Tue Jan 15 07:35:39 EET 2013
[INFO] Final Memory: 16M/309M
[INFO] ------------------------------------------------------------------------
正如我所说,一切都达到了实际需要注入的地步,nullpointerexception
之前没有错误消息。
任何人都知道什么是错的?
感谢。