@Bean yog tus qauv-theem annotation thiab ib qho ncaj qha analog ntawm XML lub caij. Cov lus piav qhia txhawb nqa feem ntau ntawm cov cwj pwm uas muab los ntawm, xws li: init-method, destroy-method, autowiring, lazy-init, dependency-check, dependency-on and scope.
Yuav siv li cas ntawm @bean annotation hauv Caij nplooj ntoos hlav khau raj?
Spring @Bean annotation qhia tias ib txoj kev tsim taum kom tswj tau los ntawm lub caij nplooj ntoo hlav. Nws yog txheej txheem-theem annotation. Thaum lub sij hawm Java configuration (@Configuration), cov txheej txheem raug tua thiab nws cov nqi xa rov qab tau sau npe ua taum hauv BeanFactory.
@bean hauv lub caij nplooj ntoo hlav yog dab tsi?
Bean txhais
Nyob rau lub caij nplooj ntoos hlav, lub khoom uas tsim cov caj qaum ntawm koj daim ntawv thov thiab uas yog tswj los ntawm lub caij nplooj ntoos hlav IoC ntim yog hu ua taum. Cov taum yog ib yam khoom uas yog instantiated, sib sau ua ke, thiab lwm yam tswj los ntawm lub caij nplooj ntoos hlav IoC thawv.
@configuration annotation ua dab tsi?
@Configuration annotation qhia tias ib chav kawm tshaj tawm ib lossis ntau dua @Bean txoj kev thiab yuav raug ua tiav los ntawm Lub thawv Caij Nplooj Ntoos Hlav los tsim cov ntsiab lus taum thiab kev pabcuam thov rau cov taum ntawm lub sijhawm ua haujlwm… Qhov no hu ua Spring Java Config feature (siv @Configuration annotation).
Cov lus piav qhia txhais li cas hauv taum nyob rau lub caij nplooj ntoo hlav?
Tsim taum siv kev tshuaj ntsuam xyuas tuaj yeem ua tiav hauv ob kauj ruam
- 1.1. Sau cov taum nrog cov ntsiab lus annotation. Peb yuav tsum siv ib qho ntawm plaub cov lus piav qhia raws li qhov tsim nyog. @Cov. …
- 1.2. suav cov pob taum hauv @ComponentScan annotation. AppConfig.java. …
- 1.3. Demo. pob com.howtodoinjava.spring;