Use XML configuration bean for spring IOC container

Source: Internet
Author: User

1. Project

2. Create an XML file

3. Printer interface

 Package com.example.demo.computerTest;  Public Interface Printer {    void  init ();     void print (String txt);}

4. Color Printer

 Package com.example.demo.computerTest;  Public class Implements Printer {    @Override    publicvoid  init () {        System.out.println (" Start the color printer! ");    }    @Override    publicvoid  print (String txt) {        System.out.println (" Print color text: ". Concat (TXT));}    }

5. Computer class

 Packagecom.example.demo.computerTest; Public classcomputer {String manu;    String type;    Printer p;  PublicString Getmanu () {returnManu; }     Public voidSetmanu (String Manu) { This. Manu =Manu; }     PublicString GetType () {returntype; }     Public voidsetType (String type) { This. Type =type; }
The Printtxt () method receives a parameter to the print () method of the printer to implement the printing function Public voidprinttxt (String txt) {p.init (); P.print (TXT); } PublicPrinter Getp () {returnp; } Public voidSetp (Printer p) { This. P =p; }}

6. Test class

Description

Loading an XML file with Classpathxmlapplicationcontext

Get the specific bean by passing in the parameter to the Context.getbean () method, which is the ID name in the XML file;

The method in the computer class can be called by instance object p to get the value set for the Computer class property in the configuration file.

 Packagecom.example.demo.computerTest;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classComputertest { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext context=NewClasspathxmlapplicationcontext ("Computers.xml"); Computer P= (computer) context.getbean ("PC"); P.printtxt ("Hello,spring!");        System.out.println (P.getmanu ());    System.out.println (P.gettype ()); }}

7. XML configuration

Description

Set the ID for each bean through the id attribute;

Setting the bean's location through the class property

You can reference a bean that has already been defined by using the ref attribute

property allows you to manipulate attributes in the bean:

The Name property specifies a property in the bean

Value sets the specified values for this property

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">       <BeanID= "Colorprinter"class= "Com.example.demo.computerTest.ColorPrinter"/>       <BeanID= "PC"class= "Com.example.demo.computerTest.Computer">           < Propertyname= "Manu">               <value>Apple</value>           </ Property>           < Propertyname= "type"value= "IPad"/>           < Propertyname= "P"ref= "Colorprinter"/>       </Bean></Beans>

8. Effect:

1, the test class can also be the following code

 Packagecom.example.demo.computerTest;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classComputertest { Public Static voidMain (string[] args) {//Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Computers.xml"); //computer p = (computer) context.getbean ("PC"); //p.printtxt ("hello,spring!"); //System.out.println (P.getmanu ()); //System.out.println (P.gettype ());ApplicationContext Context=NewClasspathxmlapplicationcontext ("Computers.xml"); Computer P= (computer) context.getbean ("PC"); P.printtxt ("Hello,spring!"); }}

2. Effect:

Use XML configuration bean for spring IOC container

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.