为什么在初始化Spring时会出现NullPointerException [英] Why do I get a NullPointerException when initializing Spring

查看:214
本文介绍了为什么在初始化Spring时会出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器上运行批处理作业时遇到了问题,而在我的开发工作站上从Eclipse运行正常。

I've got a problem running a batch job on my server, whereas it runs fine from Eclipse on my development workstation.

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

I've got my Spring environment set up using Roo, made an entity, and make a batch that does some work, and test it well on my develompent box. I initialize my context and do the work, but when I run my batch on the server, the context isn't initialized properly. Here's the code:

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

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

Any idea or hints as to what's going on? My classpath is set to $PROJECTHOME/target/classes, and all my dependencies are in $PROJECTHOME/target/lib, and I execute using "export CLASSPATH=$PROJECTHOME/target/classes; java -Djava.endorsed.dirs=$PROJECTHOME/target/lib tld.mydomain.myproject.batch.TestBatch"

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

Is there anything in my setup that looks very wrong? When I run this from Eclipse, no problems, but when I deploy it on the server where I want to run it and run it as described above, I get this problem. Because it runs from Eclipse, I believe my config files are all right, but how can I debug what's causing this? Perhaps I have some config errors or a mismatch between the server and the development workstation after all? Or is this a really weird way of saying file not found, and if so, how do I make sure it finds the correct file??

我真的很期待听取你对如何解决这个问题的建议。

I'm really looking forward to hearing your suggestions as to how to tackle this problem.

干杯

Nik

推荐答案

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

The cause of problem is -Djava.endorsed.dirs=$PROJECTHOME/target/lib org.springframework.beans.factory.support.DefaultListableBeanFactory contains the following code:

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 ,因为 getClassLoader()在通过加载类时返回 null -Djava.endorsed.dirs 。来自javadoc:

It causes a NullPointerException, because getClassLoader() returns null when class is loaded via -Djava.endorsed.dirs. From javadoc:


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

Some implementations may use null to represent the bootstrap class loader.

因此,使用 -classpath (显式指定所有jar)而不是 -Djava.endorsed.dirs

So, use -classpath (with explicit specification of all jars) instead of -Djava.endorsed.dirs

这篇关于为什么在初始化Spring时会出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆