当前位置:编程学习 > XML/UML >>

Spring载入配置文件applicationContext.xml的几种方式

第一种方式:
ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(cpr);
 
Person chinese = (Person) factory.getBean("chinese");//参数为配置文件中的id值
System.out.println(chinese.sayGoodBye("张三"));
 
 
第二中方式:(多个配置文件,用数组)
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml",
"applicationContext2.xml"});
第三种方式:(java的文件系统)
 
 
 
ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
 
Person chinese = (Person) ac.getBean("chinese");//参数为配置文件中的id值
System.out.println(chinese.sayGoodBye("张三"));
[java]  
<pre name="code" class="java"><pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
<pre></pre>  
  
</pre>  
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,