0

I'm trying to integrate Spring Dependency Injection with my Vaadin application/servlet. However the dependency injection doesn't work. I think Spring reads the components fine:

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

Since I can see my required objects like UserManager, mainWindow...

However when it comes to actual injection, it doesn't seem to work. I get nullpointerexceptions with it.

Right now I'm trying to test it with MainWindow, here's the application that should get MainWindow injected:

    @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;
    }

Yes I know I put @Autowired on both: the setter and the variable itself, however it doesn't work either way.

MainWindow:

 @org.springframework.stereotype.Component
@org.springframework.context.annotation.Scope("session")
public class MainWindow extends Window {

My 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 application-context:

 <?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>

My 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>

I didn't supply the dependencies, because I'd probably hit the character limit, however the dependencies don't seem to be lacking imho. in pom.xml I do get this error:

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)

But from command line everything seems to be compiling well:

 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] ------------------------------------------------------------------------

As I said, everything works up to the point, where it actually needs to be injected, no error messages before nullpointerexception.

Anyone knows what's wrong?

Thanks.

4

1 回答 1

0

您可能应该使用附加组件在 Vaadin 链接中使用 Spring

于 2013-01-15T13:30:59.343 回答