本篇內(nèi)容主要講解“spring @Component注解原理是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“spring @Component注解原理是什么”吧!

1.@controller 控制器(注入服務(wù))
2.@service 業(yè)務(wù)(注入dao)
3.@repository dao(實(shí)現(xiàn)dao訪問(wèn))
4.@component (把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的<bean id="" class=""/>)
5.@Component,@Service,@Controller,@Repository注解的類(lèi),并把這些類(lèi)納入進(jìn)spring容器中管理。
@Servicepublic class UserServiceImpl implements UserService {} @Repositorypublic class UserDaoImpl implements UserDao {}
6.<context:annotation-config />
這樣就可以使用@ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext、@Autowired、@Required等注解了,就可以實(shí)現(xiàn)自動(dòng)注入
7.<context:component-scan base-package="com.**.impl"/>
Spring給我們提供了context:annotation-config 的簡(jiǎn)化的配置方式,自動(dòng)幫助你完成聲明,并且還自動(dòng)搜索@Component , @Controller , @Service , @Repository等標(biāo)注的類(lèi)。
context:component-scan除了具有context:annotation-config的功能之外,context:component-scan還可以在指定的package下掃描以及注冊(cè)javabean 。還具有自動(dòng)將帶有@component,@service,@Repository等注解的對(duì)象注冊(cè)到spring容器中的功能。
因此當(dāng)使用 context:component-scan 后,就可以將 context:annotation-config移除。
8.spring ioc控制反轉(zhuǎn)
<--spring容器控制對(duì)象資源屬性--><bean id="batchNo"class="com.common.utils.IdGenerator"><constructor-arg name="datacenterId"value="${server.datacenterId}"></constructor-arg> --私有成員變量<constructor-arg name="workerId"value="${server.workerId}"></constructor-arg></bean>
<--spring依賴注入對(duì)象-->@Autowired@Qualifier("batchNo")private IdGenerator idGenerator;
相當(dāng)于使用@Component("")
到此,相信大家對(duì)“spring @Component注解原理是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文題目:spring@Component注解原理是什么-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article10/cohcdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)頁(yè)設(shè)計(jì)公司、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容