Dab tsi yog siv ntawm @autowired hauv caij nplooj ntoos hlav?
Dab tsi yog siv ntawm @autowired hauv caij nplooj ntoos hlav?

Video: Dab tsi yog siv ntawm @autowired hauv caij nplooj ntoos hlav?

Video: Dab tsi yog siv ntawm @autowired hauv caij nplooj ntoos hlav?
Video: yog yuav lwm tus ces mag xwb hhh 2024, Kaum ib hlis
Anonim

Autowiring nyob rau lub caij nplooj ntoos hlav . 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 tsis tuaj yeem siv los txhaj cov cim tseem ceeb thiab txoj hlua. Nws ua haujlwm nrog kev siv nkaus xwb.

Ntawm no, dab tsi yog kev siv Autowired annotation nyob rau lub caij nplooj ntoos hlav?

Caij nplooj ntoos hlav @ Autowired annotation yog siv rau kev txhaj tshuaj tsis siv neeg dependency. Caij nplooj ntoos hlav lub moj khaum yog tsim los ntawm kev vam khom txhaj tshuaj thiab peb txhaj cov chav kawm dependencies los ntawm caij nplooj ntoos hlav taum configuration file.

Tsis tas li ntawd, kev siv ntawm @component hauv Caij nplooj ntoos hlav yog dab tsi? Caij nplooj ntoos hlav Component annotation yog siv los qhia ib chav kawm li Cheebtsam . Nws txhais tau tias Caij nplooj ntoos hlav lub moj khaum yuav autodetect cov chav kawm no rau kev vam meej txhaj tshuaj thaum annotation-raws li configuration thiab classpath scanning yog siv.

Ib sab ntawm qhov no, Autowired nyob rau lub caij nplooj ntoos hlav yog dab tsi?

Hauv Caij nplooj ntoos hlav , koj tuaj yeem siv @ Autowired annotation rau nws pib hlau taum ntawm tus txheej txheem setter, constructor los yog ib daim teb. Ntxiv mus, nws tuaj yeem autowired vaj tse nyob rau hauv ib lub noob taum. Cov @ Autowired annotation yog nws pib xaim cov taum los ntawm kev sib piv cov ntaub ntawv hom. Saib hauv qab no puv piv txwv los qhia kev siv @ Autowired.

Dab tsi yog hom Autowiring thaum caij nplooj ntoos hlav?

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