How the configuration file is loaded in spring

Source: Internet
Author: User
Tags relative
How the configuration file is loaded in spring

There are 3 ways to load an XML configuration file in spring, and XML is the most common spring application configuration source. Several containers in spring support the use of XML assembly beans, including:
Xmlbeanfactory,
Classpathxmlapplicationcontext,
Filesystemxmlapplicationcontext,
Xmlwebapplicationcontext

One: xmlbeanfactory reference resources
Resource Resource = new Classpathresource ("Appcontext. XML ");
Beanfactory factory = new Xmlbeanfactory (Resource);

Two: Classpathxmlapplicationcontext compilation path
ApplicationContext factory=new classpathxmlapplicationcontext ("Classpath:appcontext. XML ");
ApplicationContext factory=new classpathxmlapplicationcontext ("Appcontext.   XML "); In the SRC directory
ApplicationContext factory=new classpathxmlapplicationcontext ("Conf/appcontext.   XML "); Under the src/conf directory
ApplicationContext factory=new classpathxmlapplicationcontext ("File:g:/test/src/appcontext. XML ");

Three: Using the file system path
ApplicationContext factory=new filesystemxmlapplicationcontext ("Src/appcontext. XML ");
The classpath: prefix is used as a flag, so that Filesystemxmlapplicationcontext can also read the relative path in Classpath
ApplicationContext factory=new filesystemxmlapplicationcontext ("Classpath:appcontext. XML ");
ApplicationContext factory=new filesystemxmlapplicationcontext ("File:g:/test/src/appcontext. XML ");
ApplicationContext factory=new filesystemxmlapplicationcontext ("G:/test/src/appcontext. XML ");

Four: Xmlwebapplicationcontext is specifically tailored for Web engineering.
ServletContext ServletContext = Request.getsession (). Getservletcontext ();
ApplicationContext CTX = Webapplicationcontextutils.getwebapplicationcontext (ServletContext);How to load applicationcontext.xml files in spring 1. Using Classpathxmlapplicationcontext

XML files can be read from Classpath
(1)
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Userdao 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 = (Userdao) factory.getbean ("Userdao");


2. Using Classpathresource

XML files can be read from Classpath
Resource cr = new Classpathresource ("Applicationcontext.xml"); Beanfactory bf=new Xmlbeanfactory (CR); Userdao Userdao = (Userdao) bf.getbean ("Userdao");

Loading an XML file Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer not working


3. Read with Xmlwebapplicationcontext

Xmlwebapplicationcontext CTX = new Xmlwebapplicationcontext (); Ctx.setconfiglocations (new string[] {"/web-inf/applicationcontext.xml"); Ctx.setservletcontext ( Pagecontext.getservletcontext ()); Ctx.refresh (); Userdao Userdao = (Userdao) ctx.getbean ("Userdao");


4. Read with Filesystemresource

Resource rs = new Filesystemresource ("D:/tomcat/webapps/test/web-inf/classes/applicationcontext.xml"); Beanfactory factory = new Xmlbeanfactory (RS); Userdao Userdao = (Userdao) factory.getbean ("Userdao");

It is important to note that, with Filesystemresource, the configuration file must be placed in the project direct directory, or an absolute path, or an exception will be thrown that cannot find the file.


5. Read with Filesystemxmlapplicationcontext

You can specify a relative path to the XML definition file or an absolute path to read the definition file.

Method One:
String[] path={"Webroot/web-inf/applicationcontext.xml", "Webroot/web-inf/applicationcontext_task.xml"}; ApplicationContext context = new Filesystemxmlapplicationcontext (path);

Method Two:

String path= "Webroot/web-inf/applicationcontext*.xml"; ApplicationContext context = new Filesystemxmlapplicationcontext (path);

Method Three:
ApplicationContext CTX = new Filesystemxmlapplicationcontext ("Classpath: Address");
Without Classpath, it's from the current working directory.
Spring The 3rd Chapter Spring

With spring, we don't have to write factory methods, we don't have to worry about how to assemble the code, but the reason we chose spring is not just for the IOC, but for the fact that many open source projects in Java EE now offer a way to integrate with spring. Can save us quite a lot of energy and time. 3.1. Use Spring 3.1.1 in your project . Direct construction of ApplicationContext

If we can applicationcontext.xml to classpath, we can use Classpathxmlapplicationcontext. The path passed to the parameter here is relative to the classpath configuration, for the Web project is web-inf/classes this directory, or web-inf/lib under any one of the jar file.

ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
3.1.2. Using Contextloaderlistener

Get the initialized ApplicationContext from ServletContext in Web. xml

First, configure the listener in Web. Xml.

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*: Spring*application-context.xml ");
to create a ApplicationContext object, spring first determines the path of the Conf, that is, the bin/conf directory, and then loads the configuration file from that directory by using the non-wildcard part of the path, which is CONF. Indicates that you want to load all the configuration files in the Conf directory, including all levels of subdirectories, so bin/conf/application-context.xml files and
bin/conf/admin/ The Admin-application-context.xml is loaded, and the output at spring startup is displayed as:
Loading XML Bean definitions from file
[D: Myworkspacespring-studybinconfadminadmin-application-context.xml]
Loading XML bean definitions from file
[D : Myworkspacespring-studybinconfapplication-context.xml]

2. When the project directory structure is shown in the figure:

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.