Li cas quav tshuaj txhaj ua haujlwm nyob rau lub caij nplooj ntoos hlav?

Dependency Injection

Txheej txheem cej luam

Caij nplooj ntoos hlav kaus txhaj yog ncha lug kuj hu ua ' quav tshuaj txhaj (DI)’. Yog lub plawv ntawm lub moj khaum caij nplooj ntoos hlav ' Inversion Of Control (IoC)’ DI yog yuav siv ib sab ntawm IoC. Peb yuav pib peb sib tham nrog tus assumption uas lub tswvyim thiab lub hom phiaj ntawm IoC thiab DI yog twb paub hais tias.

Nyob rau cov tshooj no, peb yuav tsom tau rua lub caij nplooj hlav kaus los nrog ib co qauv tias coding tswv yim sib txawv.








Quav tshuaj txhaj

Caij nplooj ntoos hlav kaus los yuav tsum tau rau ob txoj kev loj, ib tug yog hu ua Constructor kuas DI thiab sib ib tus raug ua DI Setter kuas. Nyob rau hauv ncua rau khej no peb yuav xyuas li no yuav tsum tiav.

Constructor raws li DI

Nyob rau hauv ib lub constructor los DI, dependencies muaj txhaj mus txog constructors. Txhua los lus cam ntawm tus constructor yog ib lub quav kwv/taum. Lub caij nplooj ntoos hlav thawv invokes lub constructors rau cov lus tsis sib.

Peb cia saib ib tug piv txwv. Muaj cov kev kawm ob, yog ib tus Lub teb chaws chav kawm ntawv thiab lwm cov ib yog cov Lub xeev hoob. Tus Lub teb chaws kawm tau ib lub quav cov Lub xeev hoob. Li ntawd, peb yuav kis ntawm quav rau cov Lub teb chaws hoob kawm los ntawm nws cov constructor (li nram qab no).

Qhia 1: Lub teb chaws cov chav kawm ntawv

pob com.techalpine.spring;

hoob tsev lub teb chaws {

lwm lub xeev State;

lub teb chaws pej xeem(Xeev xeev) {

System.out.println(“Hauv lub teb chaws constructor.” );

this.state = xeev;

}

pej xeem tsis muaj dabtsis stateName() {

state.stateName();

}

}








Tam sim no, cov quav no yuav siv tau los hu xov tooj rau txoj kev sib txawv.

Hauv qab no yog cov Lub xeev hoob chaws snippets.

Qhia 2: Cov chav kawm ntawv lub xeev

pob com.techalpine.spring;

tsev kawm ntawv xeev {

pej xeem hauv lub xeev(){

System.out.println(“Hauv lub xeev constructor.” );

}

pej xeem tsis muaj dabtsis stateName() {

System.out.println(“Hauv lub xeev lub npe: West Tsov nplooj suab”);

}

}

Hauv qab no yog cov XML configuration tej ntaub ntawv wire cov noob taum thiab lawv cov dependencies. Cov taum daim id ('stateBean') ntawm lub Lub xeev chav kawm ntawv yuav muab siv ua ib constructor tomqab siv nyob rau hauv lub Lub teb chaws hoob taum txhais. Tam sim no, cov taum no sau tseg thiab wired ua ke.

Qhia 3: Ntaub ntawv configuration XML

<?xml version =”1.0″ encoding =”UTF-8″?>

<taum 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.2.xsd”>

<!– Txhais rau teb chaws taum –>

<taum daim id =”countryBean” hoob =”com.techalpine.spring.Country”>

<constructor-arg nyob =”stateBean”/>

</taum>

<!– Txhais rau xeev taum –>

<taum daim id =”stateBean” hoob =”com.techalpine.spring.State”>

</taum>

</taum>

Hauv qab no yog cov hoob loj sim taum constructor kuas txhaj tshuaj.

Qhia 4: Tej ntaub ntawv tseem ceeb thov

pob com.techalpine.spring;

ntshuam org.springframework.context.ApplicationContext;

ntshuam org.springframework.context.support.FileSystemXmlApplicationContext;

tsev kawm TestBeanInjection {

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

ApplicationContext lub ntsiab lus teb = FileSystemXmlApplicationContext tshiab(

“WebContent/WEB-INF/beanConfig.xml”);

Teb chaws countryobj = (Lub teb chaws)context.getBean(“countryBean”);

countryobj.stateName();

}

}








Tam sim no, khiav cov chav kawm ntawv raws li ua ib daim ntawv thov stand-alone thiab nws yuav qhia cov nram qab no tso zis rau lub console.

Hauv lub xeev constructor
Hauv lub teb chaws constructor
Hauv lub xeev lub npe: West Tsov nplooj suab

Setter Bases DI

Nyob rau hauv ib lub Setter los DI, dependencies yog kis los ntawm txoj kev setter. Caij nplooj ntoos hlav thawv invokes cov kev setter los instantiate lub noob taum. Os, tus mechanism thiab lub cheebtsam uas yuav luag zoo xws li tus constructor raws li DI. Qhov txawv tsuas yog, nyob rau hauv lub tsheb dependencies thiab cov XML configuration. Nyob rau hauv ib cov txhaj raws li setter, <khoom> cim npe siv hloov <constructor-arg> lossis.








Xaus

Nyob rau cov tshooj no peb tau piav li cas lub caij nplooj hlav taum yuav tau txhaj yam. Yog tsis muaj txoj cai clear-cut thaum siv raws li setter lossis constructor raws DI. Peb feem ntau mus rau constructor raws li DI thaum tus dependencies yuav tsum, thiab xaiv DI setter kuas thaum lub dependencies yeem. Tiam sis ob peb ua ke ntawm neb ob leeg yuav tau.

 

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share