Why do I get a NullPointerException when initializing Spring

[亡魂溺海] 提交于 2019-12-01 19:00:09

问题


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

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 **/ }

}

And here's the log and exception:

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

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"

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.

Cheers

Nik


回答1:


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.
    }
}

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

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

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




回答2:


Exception in thread "main" java.lang.ExceptionInInitializerError is occurs while adding User Library. I faced same problem in Hibernate and Spring as well. So i removed User Library say "Spring" then i add jars manually it work perfectly.




回答3:


In Eclipse:

If you are using the spring jars as user library(Say SpringLib), see if the user library for spring is added(or checked) as System Library(added to the boot class path). If yes, remove the check mark.`




回答4:


Just add the jars to referenced libraries and not to user library. It worked for me!



来源:https://stackoverflow.com/questions/2272354/why-do-i-get-a-nullpointerexception-when-initializing-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!