Qhov txawv ntawm @autowired thiab @inject yog dab tsi?
Qhov txawv ntawm @autowired thiab @inject yog dab tsi?

Video: Qhov txawv ntawm @autowired thiab @inject yog dab tsi?

Video: Qhov txawv ntawm @autowired thiab @inject yog dab tsi?
Video: Qhov Txawv Ntawm Lus Hmoob Thiab Lus Mab Mawb 2024, Kaum ib hlis
Anonim

Koj tuaj yeem sau cov lus teb thiab tus tsim qauv siv @ Autowired qhia Spring moj khaum kom nrhiav tau kev vam meej rau koj. Cov @ Txhaj annotation kuj pab tib lub hom phiaj, tab sis lub ntsiab qhov sib txawv ntawm lawv yog @ Txhaj yog tus qauv annotation rau kev vam khom txhaj tshuaj thiab @ Autowired yog caij nplooj ntoos hlav tshwj xeeb.

Ib yam li ntawd, nws tau nug, siv @inject yog dab tsi?

Kev vam meej txhaj tshuaj txhawb nqa cov hom phiaj no los ntawm kev txiav txim siab qhov tsim ntawm kev siv cov khoom. Qhov ntawd ua rau koj hloov cov kev vam meej yam tsis tau hloov cov chav kawm ntawd siv lawv. Nws kuj txo qhov kev pheej hmoo uas koj yuav tsum hloov chav kawm vim tias ib qho ntawm nws qhov kev vam khom hloov pauv.

Ib sab saum toj no, 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, kev siv ntawm @autowired annotation yog dab tsi?

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 tuaj yeem yog siv 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.

Puas yog Autowiring ib qho kev txhaj tshuaj tiv thaiv?

Kev txhaj tshuaj tiv thaiv yog txhaj ib los yog ntau taum mus rau ib tug taum ntawm tus tsim los yog setter method. Caij nplooj ntoos hlav kuj muab autowired dependency txhaj tshuaj.