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

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

Video: Kev siv @value annotation nyob rau lub caij nplooj ntoos hlav yog dab tsi?
Video: qhia tshuaj zoo coj khaub ncaws dawb 28/7/2022 2024, Hlis ntuj nqeg
Anonim

Spring @PropertySource annotations yog siv los nyeem los ntawm khoom cov ntaub ntawv siv Spring's Environment interface. Cov lus piav qhia no yog nyob rau hauv kev xyaum, muab tso rau ntawm @Configuration chav kawm. Caij nplooj ntoos hlav @Value annotation tuaj yeem siv los qhia cov lus qhia ntawm daim teb lossis txoj hauv kev. Cov ntaub ntawv siv feem ntau yog qhia cov cuab yeej los ntawm a.

Raws li, kev siv ntawm @value annotation yog dab tsi?

Caij nplooj ntoos hlav @ Nqe lus piav qhia yog siv los txhaj tus nqi nyob rau hauv variables thiab method arguments. Peb tuaj yeem nyeem qhov hloov pauv ib puag ncig caij nplooj ntoos hlav lossis lub cev hloov pauv. Nws kuj txhawb SpEL.

Tsis tas li ntawd, koj yuav txhaj tshuaj Spring values li cas? Yuav ua li cas txhaj tus nqi rau hauv Bean cov khoom hauv Caij nplooj ntoos hlav

  1. Tsim ib qhov project Maven tshiab. Mus rau Cov Ntaub Ntawv -> Project -> Maven -> Maven Project.
  2. Ntxiv Caij nplooj ntoos hlav 3.2. 3 kev vam meej.
  3. Tsim ib qho yooj yim Spring Bean nrog cov khoom.
  4. XML-raws li txoj hauv kev rau kev txhaj tshuaj rau hauv cov khoom taum.
  5. Khiav daim ntawv thov.
  6. Tso zis.

Tom qab ntawd, ib tug kuj nug, dab tsi yog kev siv @value nyob rau lub caij nplooj ntoos hlav?

Caij nplooj ntoos hlav @ Tus nqi annotation yog siv los muab default tus nqi rau variables thiab method arguments. Peb nyeem tau caij nplooj ntoos hlav ib puag ncig variables raws li zoo raws li qhov system variables siv @ Tus nqi lus piav qhia. Caij nplooj ntoos hlav @ Tus nqi annotation kuj txhawb SpEL.

Yuav ua li cas koj Autowire annotation nyob rau lub caij nplooj ntoos hlav?

Feem ntau, koj yuav xav tau autowired cov cuab yeej nyob rau hauv ib qho taum tshwj xeeb nkaus xwb. Hauv Caij nplooj ntoos hlav , koj ua tau siv @ Autowired annotation rau nws pib hlau taum ntawm tus txheej txheem setter, constructor los yog 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.