最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?IOC容器Bean注解創(chuàng)建對象組件掃描

 更新時間:2022年05月30日 14:49:34   作者:把蘋果咬哭的測試筆記  
這篇文章主要為大家介紹了Spring?IOC容器Bean注解創(chuàng)建對象組件掃描,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Spring IOC Bean注解對象組件掃描

什么是注解?

注解是代碼里的特殊標記,格式:

@注解名稱(屬性名稱=屬性值, 屬性名稱2=屬性值...)

可以作用在:類、方法、屬性上面。

使用注解的目的:簡化 xml 配置,讓使用配置更簡潔優(yōu)雅。

一、spring 針對 bean 管理中創(chuàng)建對象提供注解

  • @Component
  • @Service
  • @Controller
  • @Repository

這 4 個注解功能是一樣的,都可以用來創(chuàng)建 bean 實例。

但是通常實際應用中,為了讓開發(fā)人員更加清晰當前組件所扮演的角色,一般會讓它們各自應用在不同的層。比如 @Service 用在邏輯層、@Service 用在web層等。

示例

1. 引入依賴

引入 AOP 依賴,可以在這里搜索下載需要的 jar 包。

2. 開啟組件掃描

其實就是告訴 spring 你要在什么地方使用注解。通過在 xml 里配置,spring就會到對應位置掃描下面的類:

<?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">
    <!--開啟組件掃描-->
    <context:component-scan base-package="com.pingguo.spring5.dao"></context:component-scan>
</beans>

現(xiàn)在,我這里有多個包:

如果要掃描多個包,可以用逗號,隔開:

<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)建類,并添加注解來創(chuàng)建對象

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 標簽了。

@Component(value = "userService"),這里 value 的值,等同于 <bean id="userService" ...里的 id 。@Component(value = "userService"),這里括號里的 value 可以不寫,默認就是類名稱的首字母小寫。比如 類 UserService 就是 userService 。

4. 測試一下

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();
    }
}

運行一下:

com.pingguo.spring5.service.UserService@60611244
service add() ... ...
Process finished with exit code 0

成功。

如果把注解換成其他幾個,重新運行測試方法,結(jié)果也是一樣的。

二、組件掃描的其他過濾條件

在上述的開啟掃描配置:

<!--開啟組件掃描-->
<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>

意思就是說掃描包路徑com.pingguo.spring5下的所有類。

其實這里有個屬性 use-default-filters,默認情況下就是等于true,也就是使用默認過濾規(guī)則,會去掃描路徑下的所有。

那如果use-default-filters="false",就是不使用默認過濾條件,我們可以自己配置過濾。

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>

這就是說,在路徑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>

做了改動之后,意思也變了?,F(xiàn)在是說在路徑com.pingguo.spring5下,除了Service注解的類,其他都掃描。

以上就是Spring IOC容器Bean注解創(chuàng)建對象組件掃描的詳細內(nèi)容,更多關于Spring IOC Bean注解對象組件掃描的資料請關注腳本之家其它相關文章!

相關文章

最新評論

石城县| 兴安盟| 沿河| 辰溪县| 鄂州市| 宜章县| 丽水市| 玉田县| 肥城市| 宿松县| 勃利县| 巴林左旗| 延寿县| 会宁县| 盐边县| 甘泉县| 阳曲县| 五大连池市| 蓬安县| 晋宁县| 油尖旺区| 平和县| 平阴县| 敦化市| 栾川县| 闵行区| 庆城县| 阿瓦提县| 育儿| 巴中市| 沁阳市| 娄烦县| 永川市| 西华县| 青田县| 稻城县| 大新县| 吉林市| 红河县| 淮北市| 镇康县|