[Original article, reprinted, please specify the source]
It took a lot of time to configure Spring.
First install a Spring IDE for Eclipse EE
Then download the Spring framework 3.0.6 http://www.springframework.org/
We found many dependency packages and found them on CSDN, which are all dependencies of Spring 3.0.5:
Http://download.csdn.net/detail/lzgame/3548052, a total of 8 packages
Now we need to add Spring framework and these dependency packages to the project. I made them into a User Library, as shown in figure
Do not selectSystem library (added to the boot class path)This item. Otherwise, an error will be reported during running. I will talk about it later.
Add them to the project, as shown in figure
After adding the program, you can run a simple program (the main program is shown below)
publicclassTestBatch{ private static ApplicationContext context; @SuppressWarnings("unchecked") publicstaticvoid main(finalString[] args){ context =newClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml"); try{ @SuppressWarnings("unused") TestBatch app =newTestBatch(); }catch(Exception ex){ ex.printStackTrace(); } } publicvoidTestBatch(){/** Do Something using the context **/}}
Output: Hello!
How are you?
However, this process encountered some trouble. I checkedSystem library (added to the boot class path ),The following error occurs:
Is caused here:
Caused by: java. lang. NullPointerException
At org. springframework. beans. factory. support. DefaultListableBeanFactory. <clinit> (defalistlistablebeanfactory. java: 106)
... 7 more
The source code is as follows:
static{ ClassLoader cl =DefaultListableBeanFactory.class.getClassLoader(); try{ javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");//Line 106 } catch(ClassNotFoundException ex){ // JSR-330 API not available - Provider interface simply not supported then. }}
Source code problems are:
ClassLoader
Cl = defalistlistablebeanfactory. class. getClassLoader (); return null!
I found the Internet: I found someone else has encountered the following:
Http://stackoverflow.com/questions/2272354/why-do-i-get-a-nullpointerexception-when-initializing-spring
Let's take a look at the principle of getClassLoader:
Http://blog.chenlb.com/2009/06/java-classloader-architecture.html
It turns out that the DefaultListableBeanFactory is directly loaded through BootStrap. When getClassLoader () is returned, null is returned.
JDK also describes:
"Public ClassLoaderGetClassLoader() Returns the Class Loader for this class. Some implementations may use null to indicate the bootstrap loader. If the class is loaded by the bootstrap loader, this method returns null in this implementation. "
I thought it was a mistake here: I checkedSystem library (added to the boot class path ),DefaultListableBeanFactory is directly loaded as a system Jar through BootStrap.
Just tick this one.