Understanding of Spring Dependency Injection

Source: Internet
Author: User

First of all:

1. Dependency injection is a way to define an interface in a class that needs to use it, regardless of how this interface is implemented, just execute this method

2. Dependency injection allows the class to implement generic

Code:

1. Define a beverage interface

1  PackageCn.lonecloud.model;2 /**3 * Create a food interface4 * @Title: Idrink.java5 * @Package Cn.lonecloud6 * @Description:7  * @authorLonecloud8 * @date October 4, 2016 morning 9:39:319  */Ten  Public InterfaceiDRINK { One      Public  voiddrink (); A}

2. Write its implementation class

1  PackageCn.lonecloud.model;2 3 4 /**5 * Milk6 * @Title: Milk.java7 * @Package Cn.lonecloud8 * @Description:9  * @authorLonecloudTen * @date October 4, 2016 morning 9:42:17 One  */ A  Public classMilkImplementsiDRINK { -  - @Override the      Public voiddrink () { -         //TODO auto-generated Method Stub -System.out.println ("Drink milk"); -     } +      -      +}
1  PackageCn.lonecloud.model;2 3 4  Public classWaterImplementsiDRINK {5 6 @Override7      Public voiddrink () {8         //TODO auto-generated Method Stub9System.out.println ("Drink");Ten     } One  A}
1  PackageCn.lonecloud.model;2 3  Public classCoffeeImplementsiDRINK {4 5 @Override6      Public voiddrink () {7         //TODO auto-generated Method Stub8System.out.println ("Drink is coffee");9     }Ten  One}

These three implementation classes are the implementation of this beverage interface

3. Write Customer Class

1  PackageCn.lonecloud;2 3 ImportCn.lonecloud.model.IDrink;4 5 /**6 * Customer Category7 * @Title: Customer.java8 * @Package Cn.lonecloud9 * @Description:Ten  * @authorLoneloud One * @date October 4, 2016 morning 9:47:32 A  */ -  Public classCustomer { -     /** the * Use this to inject alcohol into customers ' drinks -      * @paramMydrink -      */ -      PublicCustomer (iDRINK mydrink) { +         //TODO auto-generated Constructor stub -          This. mydrink=Mydrink; +     } A      PublicCustomer () { at         //TODO auto-generated Constructor stub -     } -      Public voidSenddrink (idrink drink) { -          This. mydrink=drink; -     } -     /** in * Customers choose a drink to drink -      */ to     PrivateiDRINK Mydrink; +     /** - * Customers drink drinks the * @Description: *      */ $      Public voiddrinking () {Panax Notoginseng Mydrink.drink (); -     } the}

The customer drinks The drink, defines the interface and can then use the definition method or constructs the method to carry on the realization to this beverage.

5. Test method

1  PackageCn.lonecloud;2 3 Importorg.junit.Test;4 5 ImportCn.lonecloud.model.Coffee;6 ImportCn.lonecloud.model.IDrink;7 8  Public classCustomertest {9     Ten     /** One * Using this kind of words can make the customer class become universal A * @Description: -      */ - @Test the      Public voidtestbyconstract () { -iDRINK drink=NewCoffee (); -         //This allows for the injection of beverages -Customer customer=NewCustomer (drink); + customer.drinking (); -     } +     /** A * Injection using the method body at * @Description: -      */ - @Test -      Public voidTestbymethod () { -iDRINK drink=NewCoffee (); -Customer customer=NewCustomer (); in         //This allows for the injection of beverages - Customer.senddrink (drink); to customer.drinking (); +     } -}

Understanding of Spring Dependency Injection

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.