为什么在初始化Spring时会出现NullPointerException

nik*_*ers 4 java spring initialization classpath spring-roo

我在服务器上运行批处理作业时遇到问题,而在我的开发工作站上从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 **/ }

}
Run Code Online (Sandbox Code Playgroud)

这是日志和例外:

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
Run Code Online (Sandbox Code Playgroud)

关于发生了什么的任何想法或暗示?我的classpath设置为$ 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运行的,所以我相信我的配置文件没问题,但是如何调试导致这种情况的原因呢?也许我有一些配置错误或服务器和开发工作站之间的不匹配?或者这是一种非常奇怪的方式来说找不到文件,如果是这样,我如何确保找到正确的文件?

我真的很期待听到你关于如何解决这个问题的建议.

干杯

axt*_*avt 7

问题的原因-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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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