Spring finishing 1-implement module Switching

Source: Internet
Author: User

The generation of objects is dynamically set by spring by reading the configuration file (applicationcontext. XML). The main adjustment class only faces Interface Programming and does not need to know the specific implementation class. In this way, we can implement class Switching Based on the configuration file. This reduces coupling. The generation of objects is completed by the framework through the reflection mechanism, and the control right is transformed from the front-end master program to the framework. This idea is called control inversion (IOC) and inverse of control, which can implement module switching well.

For example, implementing a USB simulation program

Configuration File

<Beans>

<Bean id = "disk" class = "usbdisk. usbdisk"> </bean>

</Beans>

Program

Package app;

 

Public interface idisk {

Public abstract string readinfo ();

Public abstract void writeinfo (string MSG );

}

 

Package USB disk;

 

Import app. idisk;

 

Public class usbdisk implements idisk {

Public String readinfo (){

Return "Information read from the USB flash drive ";

}

Public void writeinfo (string MSG ){

System. Out. println ("write information to the USB flash drive:" + MSG );

}

}

 

Package movedisk;

 

Import app. idisk;

 

Public class movedisk implements idisk {

Public String readinfo (){

Return "Information read from mobile hard disk ";

}

Public void writeinfo (string MSG ){

System. Out. println ("write information to the mobile hard disk:" + MSG );

}

}

 

Import java. AWT. frame;

 

Import org. springframework. Context. applicationcontext;

Import org. springframework. Context. Support. filesystemxmlapplicationcontext;

 

Import app. idisk;

 

 

Public class test {

 

 

Public static void main (string [] ARGs ){

Applicationcontext context =

New filesystemxmlapplicationcontext ("applicationcontext. xml ");

Idisk disk = (idisk) Context. getbean ("disk ");

// Instantiate the object based on the content of the configuration file and return

Disk. readinfo ();

}

}

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.