Dab tsi yog siv ntawm @autowired annotation?
Dab tsi yog siv ntawm @autowired annotation?

Video: Dab tsi yog siv ntawm @autowired annotation?

Video: Dab tsi yog siv ntawm @autowired annotation?
Video: Dej Sab Si (Maiv Ntxawm Tsab) - Av Luaj Quas - 2011 2024, Tej zaum
Anonim

Kev tshaj tawm. Cov @ Autowired annotation muab kev tswj kom zoo dua nyob qhov twg thiab yuav ua li cas autowiring yuav tsum ua kom tiav. Cov @ Autowired annotation siv tau rau autowire taum ntawm tus txheej txheem setter ib yam li @Required lus piav qhia , constructor, ib qho cuab yeej lossis txoj hauv kev nrog cov npe arbitrary thiab / lossis ntau qhov kev sib cav

Tom qab ntawd, dab tsi yog kev siv @autowired hauv lub caij nplooj ntoos hlav khau raj?

Autowiring feature ntawm caij nplooj ntoos hlav lub moj khaum enables koj mus txhaj cov khoom dependency implicitly. Nws sab hauv siv setter los yog constructor txhaj. Autowiring ua tsis tau siv txhaj tshuaj primitive thiab hlua qhov tseem ceeb. Nws ua haujlwm nrog kev siv nkaus xwb.

Tom qab ntawd, lo lus nug yog, Autowired txhais li cas? @ Autowired yog qhov pib caij nplooj ntoos hlav txhaj (ua qhov pib ntawm qhov sib txawv) qhov sib txawv raws li kev teeb tsa koj tau teev tseg hauv cov chav kawm nrog @Component annotation. Xav paub ntxiv: @ Autowired Nyob rau lub caij nplooj ntoos hlav.

Tsis tas li ntawd, Autowiring nyob rau lub caij nplooj ntoos hlav yog dab tsi?

Caij nplooj ntoos hlav nrhiav ib tug taum nrog tib lub npe raws li cov cuab yeej uas yuav tsum tau autowired . Rau piv txwv , yog taum txhais tau tias yog teem rau autowire los ntawm lub npe, thiab nws muaj ib tug tswv cuab (uas yog, nws muaj ib tug setMaster (..) txoj kev), Caij nplooj ntoos hlav nrhiav taum txhais lub npe hu ua tus tswv, thiab siv nws los teeb tsa cov cuab yeej.

Autowiring yog dab tsi thiab nws hom?

Qhov no txhais tau tias nws muaj peev xwm cia Lub Caij Nplooj Ntoos Hlav daws cov neeg koom tes (lwm cov taum) rau koj cov taum los ntawm kev tshuaj xyuas tus cov ntsiab lus ntawm tus BeanFactory. Qhov no hu ua lub caij nplooj ntoos hlav taum autowiring . Qhov autowiring functionality muaj plaub hom. Cov no yog 'tsis muaj', 'byName', 'byType' thiab 'constructor'.