0

编辑 7:

问题似乎是如何@Configurable使用HttpSessionListener,建议使用解决方法,但我不想WebApplicationContext直接与:

   @Configurable(autowire = Autowire.BY_TYPE, preConstruction = true)
    public class SessionListener implements HttpSessionListener {

        private static final Logger log;

        @Autowired
        private A a;

        @Override
        public void sessionCreated(HttpSessionEvent se) {
            a.doSomething(); // <- Throws NPE
            log.info("New session was created");
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            log.info("A session was closed");
        }
    }

编辑6:

我简化了示例项目:您现在可以从 CLI 运行它

  1. 下载解压项目:http ://www.2shared.com/file/KpS5xeqf/dynatable.html
  2. 运行 mvn clean aspectj:compile
  3. 运行 mvn gwt:run
  4. 您应该在 CLI 中看到“为什么这里的消费者为 NULL?” 被打印。
  5. 期望的是:cunsumer 不是 NULL!

快结束了:D

编辑 5:GWT 中的示例项目:请在 GWT 开发模式下运行它

http://www.2shared.com/file/eai0PV-5/dynatable.html

您将在 sessionCreated() 获得 NPE

运行要求是 maven + gwt 2.5

编辑4:

示例项目似乎不是一个很有代表性的项目。我应该重新表述问题:

在 Eclipse 中,当我将项目作为 Web 应用程序运行时,我使用 GWT 开发模式。在某种程度上,这不会调用 aspectj 编译器。至少这是我能想到的唯一原因。

问题:如何设置编译时间编织以作为 Web 应用程序运行(GWT 开发模式)。?

编辑3:

我在Eclipse 4.2.1、M2e 1.4.0、STS 3.1.0、AJDT 2.2.2中制作了一个小示例项目来演示该问题:http ://www.2shared.com/file/WZ1T9l9-/autowired.html

编辑2:

正如其他类似主题所建议的那样,我采用了标准生成Roo项目的插件以避免版本冲突。还没有成功。(更新了上面的 org.codehaus.mojo)

编辑1:

我不确定这是否正常,但是当我启动 Web 应用程序时,我得到了很长的关于 spring 正在做什么的日志,但没有提到任何编织/与 Aspectj 相关的任何内容......

我认为问题与 pom.xml 中的这个插件没有被编译有关,因为我在控制台中没有得到任何反馈(我尝试了许多其他类似的插件,但没有任何工作)当我运行网络时似乎从未调用过该插件应用:

<plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.2</version>
                    <!-- NB: do not use 1.3 or 1.3.x due to MASPECTJ-90 and do not use 1.4 
                        due to declare parents issue -->
                    <dependencies>
                        <!-- NB: You must use Maven 2.0.9 or above or these are ignored (see 
                            MNG-2972) -->
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjrt</artifactId>
                            <version>1.7.0</version>
                        </dependency>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>1.7.0</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <outxml>true</outxml>
                        <aspectLibraries>
                            <aspectLibrary>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-aspects</artifactId>
                            </aspectLibrary>
                        </aspectLibraries>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>

原帖:

我一直在搜索 stackoverflow 和许多其他资源,但他们的标准解决方案都没有帮助我找到为什么一个@autowired字段Null Pointer Exception (NPE)在访问时会屈服。

@Configurable(autowire = Autowire.BY_TYPE, preConstruction = true)
public class SessionListener implements HttpSessionListener {

    private static final Logger log;

    @Autowired
    private A a;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        a.doSomething(); // <- Throws NPE
        log.info("New session was created");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        log.info("A session was closed");
    }
}

A类声明如下:

@Service
public class B implements A {
// some implementation
}

我的应用程序上下文具有相关部分:

<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package='some.package' />

所有必要的库都在那里。我使用 Spring 3.0.2。由于我的环境,我只能使用编译时编织。我还使用 Google 插件启动 GWT 开发模式。我正在使用ADJTSpring Tool Suite和。我还为 m2e v1.0 安装了 AJDT 配置器。m2EclipseGoogle Plugin

4

1 回答 1

1

您的 App.main() 方法没有创建 ApplicationContext 开始(JUnit 测试很好,因为它使用 @ContextConfiguration 进行了注释)。为了以编程方式创建它,将以下行添加到您的 main 方法中:

public class App {      
    public static void main( String[] args ) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("foo/application-context.xml");

        new ServiceTest().doSomething();
    }
}

EDIT1:下面的静态解决方案。

缺少 2 个依赖项:

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-aspects</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
<!-- https://jira.springsource.org/browse/SPR-6819 -->
<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>persistence-api</artifactId>
  <version>1.0</version>
  <scope>provided</scope>
</dependency>

我还将ajdtBuildDefFile插件参数(该文件未包含在存档中)替换为:

<includes>
  <include>**/*.java</include>
</includes>

以这种方式调整 pom.xml 后,编译和测试通过:

mvn clean aspectj:compile test

EDIT1:此解决方案与动态编织有关,但事实并非如此。

要回答您的具体问题,请将其添加到您的 application-context.xml:

<context:load-time-weaver />

VM 还需要一个额外的运行时参数:

-javaagent:/full/path/to/spring-instrument-3.0.5.RELEASE.jar

EDIT2:关于 GWT 应用程序

坦率地说,我不确定为什么 aspectj 插件不适用于您的配置。如果您不介意快速解决方法,则只需使用 WebApplicationContextUtils:

@Override
public void sessionCreated(HttpSessionEvent se) {       
  consumer = WebApplicationContextUtils.getWebApplicationContext(se.getSession().getServletContext()).getBean(IConsumer.class);
  ...
}
于 2013-02-26T18:23:33.307 回答