How to load applicationcontext. xml files in spring

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.