Spring Framework IOC

Source: Internet
Author: User

IOC inversion control,

To cite a counter example:

1 //Data Manipulation Classes2  Public classDataBase3 {4     //to store data in a database5      Public voidsavedb ()6     {7     }8 }9 //Business logic ClassesTen  Public class Business One { A     //Defining data Interfaces -     PrivateDataBase db =NewDataBase (); -     //Data Saving the      Public voidSaveData () -     { -         //Do something else -         //Save Data + db.savedb (); -     } + } A //now modify to save to XML file at  Public classXMLData - { -     //saving data to XML -      Public voidSaveXML () -     { -     } in } - //This is the time to re-modify business logic business Logic to  Public class Business + { -     PrivateXMLData XML =NewXMLData (); the     //Data Saving *      Public voidSaveData () $     {Panax Notoginseng         //Do something Esle -         //Save Data the xml.savexml (); +     } A}

You can see that the inconvenient thing about this method is that when you change the storage mode, you need to update all the logic and methods.

//define an interface Public InterfaceSaveData () { Public voidsaveData ();}//Data Manipulation Classes Public classDataBaseImplementssavedata{//storing data in a database to implement Interface storage interfaces     Public voidSaveData () {//specific logic for storing data into a database    }}//Business logic Classes Public classbusiness{//Defining data Interfaces    PrivateSaveData DB; //defines the data save class, where the passed in parameter is actually a subclass of the interface, received with the top-level interface     Public voidSetsavedata (SaveData db) {//through the upward transformation         This. db =DB; }     Public voidSaveData () {//Do something         This. Db.savedata (); }}//Test Class Public classtestbusiness{PrivateBusiness B =NewBusiness ();  Public voidSaveData () {... b.setsavedata (NewDataBase ()); }}

The advantage now is to make business a factory where callers don't have to care about the details of the bottom store, just set the storage method to business, then call Save, and that

The extension is also made easy by first writing the XML storage method (inheriting the SaveData interface and implementing the storage method), and then business logic passing in the XML storage object without changing.

As you can see, this implementation allows code to be reused and enhanced for extensibility.

Dependency Injection: The original way is the business logic to control the specific storage, but after the interface is not written in the business logic of the specific storage mode, but in the call business logic class to control the specific storage, that is, the above, by the testbusiness call business logic, Controls the way the storage is stored (originally the business logic is set up to be stored and then called), and it appears that the business logic is controlled by the specific storage method.

Injection method:

1.set injection, which is defined as a private storage interface variable in business, then defines the set method and injects a specific storage method.

2. Interface injection, the specific storage is the same as the storage interface, but in the business logic is defined in the business logic interface, and then in this interface to define the need to inject information (such as the definition of a disavedata (SaveData db)), and then each business logic needs to inherit this interface, Then implement this storage method, on the original set method, equivalent to ensure that each business logic has a default injection method (can overwrite).

3, constructs the injection, compared with the Set method only puts the injected logic into the business construction method inside.

As you can see, you need to use interface-oriented programming, regardless of the method used

Spring Framework IOC

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.