Spring Bean loading multiple configuration files

Source: Internet
Author: User

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

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.