Caij nplooj ntoos hlav Tutorial – Tivthaiv scanning nyob Caij nplooj ntoos hlav

Component scanning in Spring framework

Caij nplooj ntoos hlav Tutorial – Tivthaiv scanning nyob Caij nplooj ntoos hlavk

Txheej txheem cej luam:

Feem ntau, programmers yuav txis cov noob taum los yog tej ntaubntawv hauv ib daim ntawv qhia txog XML configuration, Yuav kom lub caij nplooj ntoos hlav lub caij nplooj ntoos hlav lub caij nplooj ntoos hlav thiab sau npe. Txawm li cas los, Koj yuav tsum paub tias lub caij nplooj ntoos hlav yuav muaj peev xwm scanning, ntes thiab ntxiv, instantiating taum los ntawm qhov twb txhais tau tias peb tes num pob. Li ntawd, Uas yuav txuag tau koj ntawm cov tedious ted ua hauj lwm uas coj mus txis hauv ib cais XML. Qhov no yog peb yuav mus nrhiav rau hauv no tutorial.








Qhov noj qab haus huv ntawm taum?

Thaum ua hauj lwm nrog lub caij nplooj ntoos hlav, kev txhaj tshuaj ntxuav tais, cov lus sib ceg thiab tej khoom tags. Txawm li cas los, thaum peb muaj encountering loj, Nws yog cuab kev tias cov taum noob taum thiab zoo li no, Tus nqi ntawm XML yuav tsum sau ntawv ua unwieldy thiab loj.

Li ntawd, Lub caij nplooj ntoos hlav feature hu ua Auto-Wiring, ntxias, Tus nqi ntawm XML yuav muaj tsawg kawg nkaus yog tias tej kev xav noj yuav muab hais txog cov taum noob naming thiab khoom. Thaum siv no feature, naj npawb, xws li cov cuab yeej thiab tsim txom-nyiam-arg, Txo kom loj heev thiab qhov luaj li cas XML kuj yuav raug txo ntaub ntawv loj.

Thiab, Dua li ntawm qhov Disease fact tias tau tags uas yog auto-wired tseem yuav tsum txhais nyob rau lub caij nplooj ntoos hlav-configig.xml, Lub caij nplooj ntoos hlav yuav pab kom tsis txhob txhais cov ntsiab lus thiab. Yog li, Koj muaj ib txoj kev uas yuav fouabtais discovering raug.

Tus fouabtais ntawm cov taum no tswj los ntawm tej kev cai. Cov no yog –

  • Cov neeg siv ntawm lub tagnrho lub ntsiab lus teb:annotation-tag rau lub caij nplooj ntoos hlav-configig.xml caij nplooj ntoos hlav siv Annotations.
  • siv cov ntsiab lus teb:Tivthaiv-scan tag rau lub caij nplooj ntoos hlav-configig.xml qhia lub caij nplooj ntoos hlav lub hnab uas nws yuav pib-discover beans.
  • Siv cov ntawv tshaj tawm @Component khij ib hoob kawm ua tus auto-discoverable tau rau lub caij nplooj ntoos hlav.

Li ntawd, Koj yuav pom tau tias yog tus neeg @Component ntawv ces tsis tas yuav siv cov ntaub ntawv ntawd tsis tas yuav muab cov ntaub ntawv rau lub caij nplooj ntoos hlav.xml, txhij txhij XML pom kev zoo.

Vim li cas thiaj li tsis raug cai scan yog ib qho tseem ceeb?

Automatic tivthaiv yog ib qho tseem ceeb heev thaum nws pab txo tus nqi ntawm XML, Raws li tsis muaj yuav tsum tau txim txhais cov ntsiab lus nyias rau lub caij nplooj ntoos hlav-config.xml. Tseem, Txij thaum koj tsis tas yuav txis cov ntsiab lus txhais txog cov ntsiab lus nyias, nws txuag ntau lub sij hawm thiab ua rau txoj kev yooj yim.

Dab tsi yog cov annotation hom kev xyaum kom tsis txhob raug scan?

Raws li ib lub caij nplooj ntoos hla 2.5, Muaj plaub hom kev auto components scan annotations. Cov no yog –

  • @Component – Meant for auto scan tivthaiv.
  • @Repository – Indication of DAO component in the persistence txheej.
  • @Service – Qhov muab kev pab sib txuas lus sib txuas lus.
  • @Controller – Qhov no qhia hais tias tej kev tshuaj tivthaiv no tshwm sim nyob rau hauv qhov kev nthuav qhia tim khawv.

Kev siv xwb @Component ntawv tshaj tawm yuav siv tau rau lwm yam peb annotations. Li ntawd, Qhov no yog qhov zoo tshaj plaws preferred xaiv thaum sau ntawv code.

Yuav ua li cas thiaj nrhiav tau lub caij nplooj ntoos hlav?

Tag nrho cov plaub annotations uas tau raug hais saum toj no yuav muab teev rau txoj kev rho menyuam tawm thiab nrhiav tau. Txoj kev ua yooj yim li no theej yooj yim.

Koj yuav tau siv cov ntsiab lus teb:Tiv thaiv-scan tag rau lub caij nplooj ntoos hlav-cofig.xml ntaub ntawv los pab kom cov scanning feature.








Teeb ib puag ncig:

Teem tau qhov chaw rau lub caij nplooj ntoos hlav raws li txoj kev loj hlob uas yog peb kauj ruam loj.

  • Teem java kev loj hlob Java txoj kev loj hlob

Download JDK Ntawm qhov chaw Oracle thiab ces, nruab nrab thiab configure. Ces cov PATH thiab JAVA_HOME chaw yuav tsum muab teev tseg.

  • Teev ntawm Eclipse tswv yim

Eclipse yuav tsum downloaded ntawm nom website. Ib zaug downloaded, tsis them cov binaries thiab ces muab lub PATH thiab.

  • Teem caij nplooj ntoos hlav lub caij nplooj ntoos hlav

Lub caij nplooj ntoos hlav lub caij nplooj ntoos hlav (Spring libraries). http://repo.spring.io/release/org/springframework/spring/. Dua, tseem ceeb heev uas yuav tau teem caij rau CLASSPATH kom yog.

Tam sim no koj ib puag ncig yuav pib lub caij nplooj ntoos hlav Bava.

Coj daim ntawv:

Nyob rau hauv tshooj no peb yuav sau ntawv annotation raws li taum thiab mam li siv cov scan hauv cov ntaub ntawv XML los qhia cov taum lub caij nplooj ntoos hlav lub caij nplooj ntoos hlav.

Ua ntej yog lub tuam txhab pib uas muaj qhov tseem ceeb ntawm kev siv annotations.

Qhia 1: Qhov no yog lub tuam txhab taum

pob com.com techalpine.autoscan;

ntshuam org.springframework.taum... annotation.annotation.tus nqi;

ntshuam org.springframework.stereotype.Component;

@Component

pej xeem hoob Niaj hnub {

lwm yam khoom tsim kom muaj khoom;

@Value(“Tsheb me”)

lwm yam hlua prname;

@Value(“4,00,000”)

lwm yam hlua txhuv;

 

pej xeem hlua kib() {

rov qab mus nqe;

}

pej xeem khoob lug sepprrice(hlua txhuv) {

no.nqe = nqe;

}

pej xeem Khoom tau txais kev rhuav() {

rov qab mus Khoom;

}

pej xeem khoob lug setProduct(khoom tsim kom muaj khoom) {

no.khoom = yam khoom =;

}

pej xeem Hlua kis() {

rov qab mus koob meej;

}

pej xeem khoob lug setPrname(hlua prname) {

no.prname = prname;

}

}

Qhov no yog qhov khoom tau muaj yooj yim tau yooj yim thiab setter txoj kev.

Qhia 2: Qhov no yog qhov khoom pib

pob com.com techalpine.autoscan;

ntshuam org.springframework.stereotype.Component;

@Component

pej xeem hoob Khoom {

lwm yam Txoj hlua npe;

lwm yam txoj hlua txhuv;

pej xeem Txoj hlua getName() {

rov qab mus lub npe;

}

pej xeem khoob lug setName(Txoj hlua npe) {

no.lub npe = lub npe;

}

pej xeem hlua tau() {

rov qab mus nqe;

}

pej xeem khoob lug septrice(txoj hlua txhuv) {

no.nqe nqe;

}

}

Qhov no yog qhov tseem ceeb tshaj plaws uas muaj cov lus qhia rau auto-scanning ntawm components nyob rau hauv lub pob. Koj muaj peev xwm siv tau ntau yam pob los yog lwm yam pob khoom rau hauv zos kom thiaj li tsis raug txiav tawm. Nws instructs lub thawv ntim yuav nrhiav tus txheeb ze thiab ces integrate lawv li thiab thaum twg yuav tsum tau. Nws yog lub caij nplooj ntoos hlav magic thaum lub caij nplooj ntoos hlav.

Cov lus qhia syntax yog muaj li nram qab no. Nws scans tag nrho cov taum hauv qab com.com techalpine.autoscan pob.

<ntsiab lus teb:kev txhawj xeeb pob khoom noj qab zib.“com.com techalpine.autoscan” />

Qhia 3: Qhov no yog tus XML configuration ntaub ntawv

<taum xmlns =“http://www.springframework.org/schema/beans”

xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”

xmlns:ntsiab lus teb=“http://www.springframework.org/schema/context”

xsi:schemaLocation =“http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd”>

<ntsiab lus teb:kev txhawj xeeb pob khoom noj qab zib.“com.com techalpine.autoscan” />

</taum>

Qhov no yog qhov tseem ceeb los mus ntsuam xyuas daim ntawv thov kev pab.

Qhia 4: Qhov no yog lub ntsiab Java ntaub ntawv

pob com.com techalpine.autoscan;

ntshuam org.springframework.context.application;

ntshuam org.springframework.context.context.support.FileSystemPlContXAPLContx;

pej xeem hoob AutoScanMain {

pej xeem zoo li qub khoob lug ntsiab(Txoj hlua[] args) {

Application Application context = tshiab FileSystemMaXLplication(

“WebContent/WEB-INF/autoScanconfig.xml”);

Tuam txhab compob = (Niaj hnub) context.getBean(“niaj hnub”);

Tsim.tawm.println(“Khoom Lub Npe: “+compob.getPrname());

Tsim.tawm.println(“Khoom nqi: “+compob.getprrice());

}

}

Tam sim no, khiav daim ntawv thov thiab nws yuav qhia cov tso zis li nram qab no

Khoom Lub Npe: tsheb me me

Khoom nqi: 4,00,000








Xaus

Defining tau nyias nyob rau hauv cov ntaub ntawv configuration yuav muaj hectic rau tej programmer. Tivthaiv tau XML cov ntaub ntawv rau lub caij nplooj ntoos hlav yog ib feature heev uas yuav pab tau ib qho programmer hlais rau cov ntaub ntawv loj heev rau cov ntaub ntawv loj heev.

No tutorial qhia tau hais tias koj yuav setup thiab ntxiv qhia setup thiab piav seb yuav ua li cas thiaj li yuav siv cov lus tsis txaus siab uas muaj ib qho piv txwv qhia yooj yim.

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share