Cov txheej txheem:

Kev siv Autowired annotation nyob rau lub caij nplooj ntoos hlav yog dab tsi?
Kev siv Autowired annotation nyob rau lub caij nplooj ntoos hlav yog dab tsi?

Video: Kev siv Autowired annotation nyob rau lub caij nplooj ntoos hlav yog dab tsi?

Video: Kev siv Autowired annotation nyob rau lub caij nplooj ntoos hlav yog dab tsi?
Video: Test like a Pro! - Singapore Spring User Group 2024, Hlis ntuj nqeg
Anonim

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.

Khaws qhov no hauv kev txiav txim siab, dab tsi yog kev siv @autowired nyob rau lub caij nplooj ntoo 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 ua tsis tau siv txhaj tshuaj primitive thiab hlua qhov tseem ceeb. Nws ua haujlwm nrog kev siv nkaus xwb.

Ib yam 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.

Tsis tas li ntawd, kev siv 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.

Dab tsi yog cov lus piav qhia siv rau lub caij nplooj ntoos hlav?

Qee qhov tseem ceeb Spring MVC annotations yog:

  • @Controller.
  • @RequestMapping.
  • @PathVariable.
  • @RequestParam.
  • @ModelAttribute.
  • @RequestBody thiab @ResponseBody.
  • @RequestHeader thiab @ResponseHeader.

Pom zoo: