<span id="Label3"></p><p><p>A simple test with applicationcontext, get the bean instance (object) defined in Spring. you can use:</p></p><p><p>ApplicationContext ac = new Classpathxmlapplicationcontext ("applicationcontext.xml");<br>Registerdao Registerdao = (registerdao) Ac.getbean ("registerdao");</p></p><p><p>If it is more than two:<br>ApplicationContext ac = new Classpathxmlapplicationcontext (new string[]{"applicationcontext.xml", "dao.xml"});</p></p><p><p>or use wildcard characters:<br>ApplicationContext ac = new Classpathxmlapplicationcontext ("classpath:/*.xml");</p></p><p><p><br>second, Classpathxmlapplicationcontext[can Only read the configuration files placed in the web-info/classes directory] and the difference between filesystemxmlapplicationcontext</p></p><p><p>Classpath: prefix is not required, default refers to the Project's classpath path below;<br>If you want to use an absolute path, you need to add a file: prefix to indicate that this is an absolute path;</p></p><p><p>For Filesystemxmlapplicationcontext:<br>The default represents two Types:</p></p><p><p>1. No drive letter is the project work path, that is, the root directory of the project;<br>2. A drive symbol indicates the absolute path of the File.</p></p><p><p>If you want to use the classpath path, prefix classpath is required:</p></p><p><p>public class Helloclient {</p></p><p><p>Protected static final Log log = Logfactory.getlog (helloclient.class);</p></p><p><p>public static void main (string[] Args) {<br>Resource Resource = new Classpathresource ("appcontext.xml");<br>Beanfactory factory = new Xmlbeanfactory (resource);</p></p><p><p>Using the Classpath path<br>ApplicationContext factory = new Classpathxmlapplicationcontext ("classpath:appcontext.xml");<br>ApplicationContext factory = new Classpathxmlapplicationcontext ("appcontext.xml");</p></p><p><p>Classpathxmlapplicationcontext uses a file prefix that can use an absolute path.<br>ApplicationContext factory = new Classpathxmlapplicationcontext ("file:f:/workspace/example/src/appcontext.xml");</p></p><p><p>The path to the file system, which is the root path of the project by default<br>ApplicationContext factory = new Filesystemxmlapplicationcontext ("src/appcontext.xml");<br>ApplicationContext factory = new Filesystemxmlapplicationcontext ("webroot/web-inf/appcontext.xml");<br><br></p></p><p><p>The classpath: prefix is used so that filesystemxmlapplicationcontext can also read the relative path under Classpath<br>ApplicationContext factory = new Filesystemxmlapplicationcontext ("classpath:appcontext.xml");<br>ApplicationContext factory = new Filesystemxmlapplicationcontext ("file:f:/workspace/example/src/appcontext.xml") ;</p></p><p><p>Do not add a file prefix<br>ApplicationContext factory = new Filesystemxmlapplicationcontext ("f:/workspace/example/src/appcontext.xml");<br>Ihelloworld HW = (ihelloworld) Factory.getbean ("helloworldbean");<br>Log.info (hw.getcontent ("luoshifei"));<br>}<br>}</p></p><p><p>Notes on ApplicationContext in spring</p></p></span>
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