【问题标题】:Why do I get a NullPointerException when initializing Spring为什么初始化 Spring 时会出现 NullPointerException
【发布时间】:2010-02-16 11:09:07
【问题描述】:

我在我的服务器上运行批处理作业时遇到问题,而它在我的开发工作站上的 Eclipse 中运行良好。

我已经使用 Roo 设置了我的 Spring 环境,创建了一个实体,并创建了一个可以执行某些工作的批处理,并在我的开发盒上对其进行了很好的测试。我初始化我的上下文并完成工作,但是当我在服务器上运行我的批处理时,上下文没有正确初始化。代码如下:

public class TestBatch {

    private static ApplicationContext context;


    @SuppressWarnings("unchecked")
    public static void main(final String[] args) {

            context = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
            try {
                @SuppressWarnings("unused")
                TestBatch app = new TestBatch();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }

    public void TestBatch() { /** Do Something using the context **/ }

}

这里是日志和异常:

2010-02-16 11:54:16,072 [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6037fb1e: startup date [Tue Feb 16 11:54:16 CET 2010]; root of context hierarchy
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:458)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:388)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at tld.mydomain.myproject.batch.TestBatch.main(TestBatch.java:51)
Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:103)
    ... 7 more

关于发生了什么的任何想法或提示?我的类路径设置为 $PROJECTHOME/target/classes,我所有的依赖项都在 $PROJECTHOME/target/lib 中,我使用“export CLASSPATH=$PROJECTHOME/target/classes; java -Djava.endorsed.dirs=$PROJECTHOME /target/lib tld.mydomain.myproject.batch.TestBatch"

我的设置中是否有任何看起来非常错误的内容?当我从 Eclipse 运行它时,没有问题,但是当我将它部署在我想要运行它的服务器上并如上所述运行它时,我遇到了这个问题。因为它是从 Eclipse 运行的,所以我相信我的配置文件没问题,但是我该如何调试导致这种情况的原因呢?也许我有一些配置错误或服务器和开发工作站之间不匹配?或者这是说文件未找到的一种非常奇怪的方式,如果是这样,我如何确保它找到正确的文件??

我非常期待听到您关于如何解决这个问题的建议。

干杯

尼克

【问题讨论】:

  • 我建议查看 DefaultListableBeanFactory.java,第 103 行。或者更好的是,在此处设置断点。
  • 确保applicationContext.xml 在服务器上就位。
  • applicationContext 在 $CLASSPATH/META-INF/spring/applicationContext.xml 中至于设置断点,在我的服务器上运行时我该怎么做呢?当我在我的工作站上通过我的 IDE 运行它时没有错误
  • $CLASSPATH 实现为什么? webapp/WEB-INF/classes/,我猜?

标签: java spring initialization classpath spring-roo


【解决方案1】:

问题原因是-Djava.endorsed.dirs=$PROJECTHOME/target/lib org.springframework.beans.factory.support.DefaultListableBeanFactory 包含以下代码:

static {
    ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
    try {
        javaxInjectProviderClass = cl.loadClass("javax.inject.Provider"); //Line 103
    }
    catch (ClassNotFoundException ex) {
        // JSR-330 API not available - Provider interface simply not supported then.
    }
}

它会导致NullPointerException,因为当通过-Djava.endorsed.dirs 加载类时getClassLoader() 返回null。来自 javadoc:

某些实现可能使用 null 来表示引导类加载器。

所以,使用-classpath(明确指定所有罐子)而不是-Djava.endorsed.dirs

【讨论】:

  • 非常感谢,为我解决了这个问题。但后来我又回到了一个巨大的类路径!有没有办法让我的类路径保持整洁并仍然让我的程序正确执行?
  • 真的不需要担心类路径是什么样子的。它只是传递给程序的一个参数。您甚至很少需要手动设置它,大多数应用程序通过脚本或其他方法来处理它。
  • 这也可能是 Rational Application Developer/Eclipse 中的一个问题,您已将 jar 添加到标记为系统库的用户库中。真是松了一口气,我认为 Spring 肯定不会发布像 spring-test 框架这样在 RAD 中不起作用的东西……原来这是我的问题。
  • 我自己刚刚遇到了这个问题。 Ryan 关于不让 spring 成为系统用户库的提示解决了我的问题,非常感谢!
  • 如何执行您的答案中指定的这些步骤我没有得到您想说的内容
【解决方案2】:

线程“main”java.lang.ExceptionInInitializerError在添加用户库时发生异常。我在 HibernateSpring 中也遇到了同样的问题。所以我删除了 User Library 说“Spring”,然后我手动添加了 jars 它工作得很好。

【讨论】:

    【解决方案3】:

    在 Eclipse 中:

    如果您使用spring jars作为用户库(例如SpringLib),请查看spring的用户库是否为added(or checked)作为系统库(添加到启动类路径)。如果是,remove 复选标记。`

    【讨论】:

      【解决方案4】:

      只需将 jars 添加到引用库而不是用户库。它对我有用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-06
        • 2014-05-03
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多