From: http://blog.csdn.net/hsliwei/article/details/7239030
1. Use classpathxml Applicationcontext
FromClassPathRead XML files
(1)
Applicationcontext context = new classpathxmlapplicationcontext ("applicationcontext. xml"); userdao = (userdao) Context. getbean ("userdao ");
(2)
Classpathxmlapplicationcontext resource = new classpathxmlapplicationcontext (New String [] {"applicationContext-ibatis-oracle.xml", "applicationcontext. XML "," applicationContext-data-oracle.xml "}); beanfactory factory = resource; userdao = (userdao) factory. getbean ("userdao ");
2. Use classpathresource
From classpathRead XML files
Resource Cr = new classpathresource ("applicationcontext. xml"); beanfactory BF = new xmlbeanfactory (CR); userdao = (userdao) BF. getbean ("userdao ");
Load an XML file Org.SpringFramework. Beans. Factory. config. propertyplaceholderconfigurer does not work
3. Use xmlwebapplicationcontext to read
Xmlwebapplicationcontext CTX = new xmlwebapplicationcontext (); CTX. setconfiglocations (New String [] {"/WEB-INF/applicationcontext. XML "); CTX. setservletcontext (pagecontext. getservletcontext (); CTX. refresh (); userdao = (userdao) CTX. getbean ("userdao ");
4. Read data using filesystemresource
Resource rs = new filesystemresource ("D:/tomcat/WebApps/test/WEB-INF/classes/applicationcontext. xml "); beanfactory factory = new xmlbeanfactory (RS); userdao = (userdao) Factory. getbean (" userdao ");
It is worth noting that when filesystemresource is used, the configuration file must be placed in the direct directory of the project, or the absolute path must be specified. Otherwise, an exception that cannot be found will be thrown.
5. Use filesystemxmlapplicationcontext to read
You can specify the relative or absolute path of the XML definition file to read the definition file.
Method 1:
String [] Path = {"webroot/WEB-INF/applicationcontext. xml", "webroot/WEB-INF/applicationcontext_task.xml"}; applicationcontext context = new filesystemxmlapplicationcontext (PATH );
Method 2:
String Path = "webroot/WEB-INF/applicationcontext *. xml"; applicationcontext context = new filesystemxmlapplicationcontext (PATH );
Method 3:
Applicationcontext CTX = new filesystemxmlapplicationcontext ("classpath: Address ");
If there is no classpath, it is from the current working directory.