Spring?IOC容器Bean注解創(chuàng)建對(duì)象組件掃描
Spring IOC Bean注解對(duì)象組件掃描
什么是注解?
注解是代碼里的特殊標(biāo)記,格式:
@注解名稱(屬性名稱=屬性值, 屬性名稱2=屬性值...)
可以作用在:類、方法、屬性上面。
使用注解的目的:簡(jiǎn)化 xml 配置,讓使用配置更簡(jiǎn)潔優(yōu)雅。
一、spring 針對(duì) bean 管理中創(chuàng)建對(duì)象提供注解
- @Component
- @Service
- @Controller
- @Repository
這 4 個(gè)注解功能是一樣的,都可以用來(lái)創(chuàng)建 bean 實(shí)例。
但是通常實(shí)際應(yīng)用中,為了讓開(kāi)發(fā)人員更加清晰當(dāng)前組件所扮演的角色,一般會(huì)讓它們各自應(yīng)用在不同的層。比如 @Service 用在邏輯層、@Service 用在web層等。
示例
1. 引入依賴
引入 AOP 依賴,可以在這里搜索下載需要的 jar 包。
2. 開(kāi)啟組件掃描
其實(shí)就是告訴 spring 你要在什么地方使用注解。通過(guò)在 xml 里配置,spring就會(huì)到對(duì)應(yīng)位置掃描下面的類:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--開(kāi)啟組件掃描--> <context:component-scan base-package="com.pingguo.spring5.dao"></context:component-scan> </beans>
現(xiàn)在,我這里有多個(gè)包:
如果要掃描多個(gè)包,可以用逗號(hào),隔開(kāi):
<context:component-scan base-package="com.pingguo.spring5.dao, com.pingguo.spring5.service"></context:component-scan>
如果所有下層的包都要掃描,那也可以之間寫上層的目錄:
<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>
3. 創(chuàng)建類,并添加注解來(lái)創(chuàng)建對(duì)象
package com.pingguo.spring5.service; import org.springframework.stereotype.Component; @Component(value = "userService") public class UserService { public void add() { System.out.println("service add() ... ..."); } }
現(xiàn)在終于不用去 xml 寫 bean 標(biāo)簽了。
@Component(value = "userService"),這里 value 的值,等同于 <bean id="userService" ...里的 id 。@Component(value = "userService"),這里括號(hào)里的 value 可以不寫,默認(rèn)就是類名稱的首字母小寫。比如 類 UserService 就是 userService 。
4. 測(cè)試一下
package com.pingguo.spring5.testdemo; import com.pingguo.spring5.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestService { @Test public void testService() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); System.out.println(userService); userService.add(); } }
運(yùn)行一下:
com.pingguo.spring5.service.UserService@60611244 service add() ... ... Process finished with exit code 0
成功。
如果把注解換成其他幾個(gè),重新運(yùn)行測(cè)試方法,結(jié)果也是一樣的。
二、組件掃描的其他過(guò)濾條件
在上述的開(kāi)啟掃描配置:
<!--開(kāi)啟組件掃描--> <context:component-scan base-package="com.pingguo.spring5"></context:component-scan>
意思就是說(shuō)掃描包路徑com.pingguo.spring5下的所有類。
其實(shí)這里有個(gè)屬性 use-default-filters,默認(rèn)情況下就是等于true,也就是使用默認(rèn)過(guò)濾規(guī)則,會(huì)去掃描路徑下的所有。
那如果use-default-filters="false",就是不使用默認(rèn)過(guò)濾條件,我們可以自己配置過(guò)濾。
1. include-filter
在指定的包路徑下,只掃描包含了某種注解的類。比如:
<context:component-scan base-package="com.pingguo.spring5" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
這就是說(shuō),在路徑com.pingguo.spring5下,只掃描Service注解的類。
2. exclude-filter
與上面相反,這里是除了xx之外,都去掃描。
<context:component-scan base-package="com.pingguo.spring5" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
做了改動(dòng)之后,意思也變了。現(xiàn)在是說(shuō)在路徑com.pingguo.spring5下,除了Service注解的類,其他都掃描。
以上就是Spring IOC容器Bean注解創(chuàng)建對(duì)象組件掃描的詳細(xì)內(nèi)容,更多關(guān)于Spring IOC Bean注解對(duì)象組件掃描的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis使用resultMap如何解決列名和屬性名不一致
這篇文章主要介紹了MyBatis使用resultMap如何解決列名和屬性名不一致的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用
這篇文章主要為大家介紹了微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn)
本篇文章主要介紹了詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn),我們可以使用重定向的方式,改變?yōu)g覽器的地址欄,防止表單因?yàn)樗⑿轮貜?fù)提交。有興趣的可以了解一下。2017-01-01java實(shí)現(xiàn)注冊(cè)登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)注冊(cè)登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04RabbitMQ實(shí)現(xiàn)消息可靠性傳遞過(guò)程講解
消息的可靠性傳遞是指保證消息百分百發(fā)送到消息隊(duì)列中去,這篇文章主要介紹了RabbitMQ實(shí)現(xiàn)消息可靠性傳遞過(guò)程,感興趣想要詳細(xì)了解可以參考下文2023-05-05mybatis相同的sql查詢第二次查不出結(jié)果問(wèn)題
這篇文章主要介紹了mybatis相同的sql查詢第二次查不出結(jié)果問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01kafka內(nèi)外網(wǎng)訪問(wèn)配置方式
這篇文章主要介紹了kafka內(nèi)外網(wǎng)訪問(wèn)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09JavaWeb建立簡(jiǎn)單三層項(xiàng)目步驟圖解
這篇文章主要介紹了JavaWeb建立簡(jiǎn)單三層項(xiàng)目步驟圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07