In addition to writing a very simple loading of an XML, loading a number of cases has been useless, in the company will not be handled by themselves this problem, now need to use, research and verification.
The case used is also the example above.
Only, divide the original beans.xml into two parts.
1. Structure
2.beans.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5 6 <!--7 load an XML, this as the total XML entry8 -9 <ImportResource= "Beans2.xml"/>Ten One <BeanID= "HelloWorldService"class= "Com.it.bean.HelloWorldService"> A < Propertyname= "HelloWorld"ref= "Springhelloworld"/> - </Bean> - the - </Beans>
3.beans2.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5 6 <BeanID= "Springhelloworld"class= "Com.it.service.SpringHelloWorld"></Bean>7 8 </Beans>
4. Run the class
1 PackageCom.it.main;2 3 ImportCom.it.service.HelloWorld;4 ImportCom.it.bean.HelloWorldService;5 ImportOrg.springframework.context.ApplicationContext;6 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;7 8 Public classHellomain {9 Public Static voidMain (string[] args) {Ten OneApplicationContext context =NewClasspathxmlapplicationcontext ("Beans.xml"); A -HelloWorldService service = (helloworldservice) context.getbean ("HelloWorldService"); - theHelloWorld hw=Service.gethelloworld (); - - Hw.sayhello (); - } +}
Spring Bean loading multiple configuration files