Yuav ua li cas sau ntawv Java annotations?

Java annotations

Nyob rau cov tshooj no peb yuav tham txog annotations thiab seb lawv yuav siv tswv yim programming. Ua ntej, peb yuav sib tham txog tej tswv yim conceptual thiab ho dhia rau coding sau ib qho piv txwv kev siv annotations.

Sab.:

Nyob rau hauv Java programming, muaj ntau txoj kev los txis tus cwj pwm uas nyias hais muaj (zoo li txoj kev, hoob, neeg thiab lwm yam). Feem ntau ntawm cov raug tus mob no cov ntaub ntawv twb txhais muaj nyob rau hauv tej ntaub ntawv uas lwm (XML, khoom cov ntaub ntawv thiab lwm yam). Thiab ces cov ntaub ntawv yuav raug xa mus rau hauv qhov kev pab nws yuav sau.

Thaum annotation, yog qhov kev txawv lig. Annotations no meta-cov ntaub ntawv thiab cov sau ua ib hom kev. Cov annotation hais sau nyob rau hauv txoj kev Java pab nws tus kheej. Li ntawd, yuav tsis muaj los xa tej ntaub ntawv uas lwm, Nws yog ib tug loj kom zoo dua. Cov annotations yog ib qho yooj yim mus use thiab heeev kom muaj kev.

Nyob rau hauv cov seem tom ntej no peb yuav pom li cas no yuav muab los siv.








Java annotation yog dab tsi?

Ib Java annotation yog ib metadata ntawm ib qho kev pab Java. Los sis, tsuas hais lus, ib Java annotation qhia cov ncauj lus txog ib qho kev pab Java. Feem ntau, nws tsis ncaj feem xyuam rau txoj cov kev pab cuam los yog chaws.

Vim li cas Java annotations yog siv?

Ib Java annotation yuav siv tau rau cov laj thawj nram no:

  • Muab lus qhia rau cov compiler

Thaum twg tus compiler converts tus ua machine-readable qhia, annotations tau muab cov compiler tej lus qhia. Feem ntau, tom qab muab tso ua ke, tus annotations yuav tsis nyob hauv qhov chaws.

  • Muab cov lus qhia rau cov builder

Hnub no, koj yuav pub lwm yam cuab yeej xws li Apache Maven los yog Apache ntsaum tsim tus. Annotations yuav ua rau muaj lwm yam cuab yeej pab tau. Thaum twg cov cuab yeej muaj raws li lawv cov paub tab uas muaj generating qhov chaws, generating XML ntaub ntawv (e.g. txiag descriptors), cov ntaub ntawv rau lub THAWV ntawv ntim cov lis dej num thiab cov, nws zoo nkaus li rau cov annotations rau inputs nyob generating qhov chaws los yog ntaub ntawv.

  • Muab lus qhia rau cov software runtime

Tej zaum yuav txhais tau annotations txog kev inputs rau software runtime txawm tias tej annotations yuav tsum accessed nrog Java Reflections, ib tug uas txawv.

Annotations yuav tau ob yam predefined thiab kev cai. Thaum kev cai annotations yuav muaj ntau yam, predefined annotations yog muaj peb hom li nram no.








@Deprecated annotation ntawd hais tias ib lub chav kawm ntawv, qauv los yog daim teb uas yog tsis siv.

@Override annotation indicates tias ib hoob kawm nyob rau hauv ib txoj kev metch tsis phim tej kev superclass. Thaum ib tug neeg mismatch, lub compiler throws ib qho yuam kev.

@SuppressWarnings annotation supresses lus ceeb toom ces muab pov tseg los ntawm lub compiler. Piv txwv, yog ib txoj kev uas hu rau deprecated kev deprecated, lub compiler throws ib ceeb toom. Cov annotation supresses tej lus ceeb toom thiab ntxiv.

Tsim peb tes num thawj peb tes num:

Nyob rau hauv tshooj no peb yuav tsim ib qhov project uas siv Eclipse IDE. Qhov chaws yuav qhia li cas annotation ua haujlwm nyob rau hauv ib cheeb tsam tiag tiag. Ua ntej yuav tsiv mus nyob rau hauv tes-on coding peb yuav tsum tau tiav cov plahaum nruab nrab kom cov ib puag ncig.

Tom qab no tus plahaum plahaum.

Kauj ruam 1: Teem java txoj kev loj hlob Java

Download JDK Ntawm qhov chaw Oracle, nruab nrab thiab configure. Ces teem lub PATH thiab JAVA_HOME ntawm tej variables.

Kauj ruam 2: Teem caij mus Eclipse tswv yim

Download Eclipse ntawm qhov nom website. Nruab nrab thiab muab txoj kev pathriable.

Kauj ruam 3: Download annotation tej pob khoom ntawm caij nplooj ntoos hlav qiv.

Nyob rau hauv peb piv txwv peb yuav tau siv cov ntaub ntawv txhais lus spring. Download org.springframework.context.jar saum toj noj txuas thiab muab tso rau hauv txuas folder hauv lub tswv yim Eclipse li qhia hauv qab no.

Spring packages

Caij nplooj ntoos hlav tej pob khoom

Daim Duab: Annotation Jars hauv liber folder

Tam sim no peb cheeb tsam yog npaj sau Java programs nrog annotations.

Cia peb tsim tawm thawj qhov project uas siv Eclipse IDE.

Tsim ib qhov project hauv Eclipse thiab teeb lub npe ua pob com.com techalpine..annotation.annotation. Nyob rau hauv cov pob no tsim peb Java hoob kawm raws li muaj hauv qab no.

Project Structure

Ib qhov qauv

Daim Duab: Peb tes num

Peb hoob kawm yog Beannotation.java, BeannotationConfig.java thiab BeanAnnotationMain.java

Listing1: Qhov no yog cov hoob kawm uas muaj cov hoob kawm thiab setter methods

[chaws]

pob.com.techalpine.annotation.annotation;

Pej xeem hoob kawm Beannotation {

ntiav lus;

pej xeem tsis txhob setMessagetx(txoj hlua lus){

qhov no hais kom meej = cov lus;

}

pej xeem void getMessagetx(){

System.out.println(“Qhov no yog hais : ” + messagetxt);

}

}

[/chaws]

Ntxiv mus yog qhov configuration class nrog @Configuration Thiab @Taum kev ntxub ntxaug. Cov annotations no yuav qhia tau hais tias yog li cas thiab qhia rau lub thawv rau hauv tsev.

Listing2: Qhov no yog qhov configuration hoob kawm

[chaws]

pob.com.techalpine.annotation.annotation;

import.springframework.context.*;

@Configuration

Pej xeem hoob kawm BeannotationConfig {

@Bean

Pej xeem Beannotation pibannotate(){

rov qab tshiab Beannotation();

}

}

[/chaws]







Ntxiv mus yog cov hoob kawm los ntsuam xyuas cov annotations thiab seb nws ua haujlwm li cas.

Listing3: Qhov no yog qhov tseem ceeb chav kawm ntawv

[chaws]

pob.com.techalpine.annotation.annotation;

ntshuam org.springframework.context.ApplicationContext;

import.springframework.context.*;

Pej xeem hoob kawm BeannotationMain {

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

Application Application cxtx =

AnnotationConFigaplication(BeannotationConfig.class);

Taum beannotation = ctx.getBean(Beannotation.chav kawm ntawv);

beannotate..setMessagetx(“Nyob zoo, Annotation ua hauj lwm zoo!”);

taum.getMessagetx();

}

}

[/chaws]








Executing peb tes num:

Peb tes num yog tiav thiab nws yog npaj tiav. Tam sim no khiav daim ntawv thov raws li hauv qab no. Nws yuav ua tau ib tug stand-alone Java, Ces nws yuav tsum tau khiav raws li daim ntawv Thov Java li qhia hauv qab no.

Run application

Daim ntawv thov cia

Daim Duab: Khiav daim ntawv thov kev pab

Thaum twg qhov kev pab cuam tseem tiav lawm, nws yuav qhia cov tso zis.

Output

Tso zis

Daim Duab: Uas qhia tso zis rau cov console

Xaus:

Nyob rau hauv tsab xov xwm no peb tau tham txog lub tswvyim ntawm annotation thiab li cas nws yuav tau DVR. Vam tias nws yuav pab cov neeg siv los sau Java cov kev pab cuam nrog annotations.

 

Tagged: ,
============================================= ============================================== 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