Spring Dependency Injection

Source: Internet
Author: User

Watching TV for a day in the daytime, one is because it is too hot, impatient the air to see the book, second, because this TV is pretty good, Zu Feng Woodstock Hou Yong. The Mask of the Act. TV good-looking, their efforts to make money to study hard to learn is the reality of the night and quarrel, really is a small noisy day, two days a big noisy; Reading, learning, appreciation, pay rise is the kingly way!!!

Dependency injection also feel difficult to understand the appearance, in fact, now feel good, do not give yourself to imply that this difficult, that difficult; in fact, some things just look difficult, and so you really calm down to see him, once again do not understand, see two times, two times do not understand three times, always a little feeling. Another thing is, when you learn a new point of knowledge, you first accept him, do not start to doubt him, so it is difficult to understand this thing, it is easy to get into the mire!!!

Study Address: https://www.w3cschool.cn/wkspring/t7n41mm7.html

Two methods of dependency injection:

1) Dependency injection based on constructor function

2) Dependency injection based on setter method

constructor-based Dependency injection

The instance you use is a text editor that calls the spelling checker for the spelling checker;

Spellchecker.java: There's a way to spell check in the spell checker class

 Package Com.lee.instructor;  Public class spellchecker {    public  spellchecker () {        System.out.println ("Inside spellchecker Constructor ");    }      Public void checkspelling () {        System.out.println ("Inside checkspelling.") );    }}

Texteditor.java: This is the editing class, in the constructor to complete the dependency with the spellchecker;

 Package Com.lee.instructor;  Public class texteditor {    private  spellchecker spellchecker;      Public TextEditor (spellchecker spellchecker) {
System.out.println ("Inside TextEditor constructor."); this. spellchecker = spellchecker; } Public void spellcheck () { spellchecker.checkspelling (); }}

When a container invokes a class constructor with a set of parameters, the constructor-based di is completed, where each parameter represents a dependency on another class. My understanding:

Public TextEditor (spellchecker spellchecker) Here is TextEditor and the Spellchecker class to establish a connection.

beans.xml: Configuration based on constructor injection

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "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 ">    class=" Com.lee.instructor.TextEditor ">        <constructor-arg ref= "Spellchecker" ></constructor-arg>    </bean>    class= " Com.lee.instructor.SpellChecker ">    </bean></beans>

Dependency injection based on set function

Spellchecker.java:

 Package Com.lee.another;  Public class spellchecker {    public  spellchecker () {        System.out.println ("Inside spellchecker constructor. " );    }          Public void checkspelling () {        System.out.println ("Inside checkspelling.") );    }}

Texteditor.java:

 PackageCom.lee.another; Public classTextEditor {Privatespellchecker spellchecker;  Publicspellchecker Getspellchecker () {returnspellchecker; }     Public voidSetspellchecker (spellchecker spellchecker) {System.out.println ("Inside Setspellchecker");  This. Spellchecker =spellchecker; }     Public voidspellcheck () {spellchecker.checkspelling (); }}

When the container invokes a parameterless constructor or an argument-free static factory method to initialize the bean, the function-based di is completed by invoking the Set function on the bean.

Mainapp.java:

 PackageCom.lee.another;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans2.xml"); TextEditor TX= (TextEditor) context.getbean ("TextEditor");    Tx.spellcheck (); }}

Beans2.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "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 ">    class=" Com.lee.another.TextEditor ">        <property name=" Spellchecker "ref=" Spell "></property>    </bean>    class=" Com.lee.another.SpellChecker ">    </bean></beans>

Property properties in the bean:

name: Represents the Spellchecker parameter in the TextEditor class;

ref: Reference an already existing object that can reference another Bean object:

Value: Create a new object; You can assign values of some simple types

Inject internal beans:

This is a beans.xml file, the rest of the same, a bean embedded into another bean, such as:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "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 ">    class=" Com.lee.third.TextEditor ">        <property name=" Spellchecker ">            class=" Com.lee.third.SpellChecker "> </bean>        </property>    </bean></beans>

Code Cloud: Https://gitee.com/lemon_le/w3-Spring/tree/master/DI

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.