1. 程式人生 > >【Spring註解】@Configuration和@ComponentScan註解

【Spring註解】@Configuration和@ComponentScan註解

Spring註解

一、組建註冊

包結構:

image

[email protected]和@ComponentScan

@ComponentScan的includeFilters用法

FilterType的型別

ANNOTATION,基於註解的過濾
ASSIGNABLE_TYPE,指定class
ASPECTJ,ASPECTJ表達shi
REGEX,正則表示式
CUSTOM;自定義
//相當於application-bean.xml
@Configuration 
//配置掃描包的策略,當前註解為只掃描加@Controller的元件
//注意useDefaultFilters要設定為fasle,不然為預設的Filters,@Service/@Repository都掃描
@ComponentScan(value = "com.gaoyuzhe",includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class) }, useDefaultFilters = false)

執行結果

9fmJS0.png

excludeFilters用法:

注意:useDefaultFilters設定為true,不然不會掃描到元件

package com.gaoyuzhe.config;

import
com.gaoyuzhe.pojo.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; /** * @author
GaoYuzhe * @date 2018/3/12. */
@Configuration @ComponentScan(value = "com.gaoyuzhe",excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class) }, useDefaultFilters = true) public class MainConfig { @Bean public Person person(){ return new Person("gaoyuzhe ",12); } }

執行結果

excludeFilters用法

自定義Filter策略

/**
 * 自定義的過濾規則
 * @author GaoYuzhe
 * @date 2018/3/12.
 */
public class MyTypeFilter implements TypeFilter {
    /**
     * 匹配規則
     * @param metadataReader the metadata reader for the target class
     *                       當前類的資訊
     * @param metadataReaderFactory  a factory for obtaining metadata readers
     *                                for other classes (such as superclasses and interfaces)
     *                               當前類的父類或者介面的資訊
     * @return 是否匹配
     * @throws IOException
     */
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //當前類名稱
        String className = classMetadata.getClassName();
        System.out.println("className = " + className);
        //當前類的資源路徑
        Resource resource = metadataReader.getResource();
        System.out.println("resource = " + resource);
        //如果類名包含"Dao",則為匹配成功
        return className.contains("Dao");
    }
}

配置類註解。

 @Configuration
 @ComponentScan(value = "com.gaoyuzhe",includeFilters = {
         @ComponentScan.Filter(type = FilterType.CUSTOM,classes = MyTypeFilter.class)
 }, useDefaultFilters = false)

執行結果
執行結果

相關推薦

Spring註解@Configuration@ComponentScan註解

Spring註解 一、組建註冊 包結構: [email protected]和@ComponentScan @ComponentScan的includeFilters

Spring MVC註解配置檔案的程式碼比較

當我們在類檔案裡寫了方法,怎麼被程式知道並呼叫呢?一般有兩種方法: 配置檔案 註解 下面小編就以親自敲的例子“SpringMVC_Test”為例來簡單說說。 配置檔案篇 在springmvc.xml中這樣寫: 在controller包下的類中這樣寫: 註

SpringSpringBoot中的@Component @ComponentScan註解用法介紹注意事項

通過本文你將學到: Component Scan是什麼? 為什麼ComponentScan很重要? 專案中Spring Boot會對哪些包自動執行掃描(Component Scan)? 如何利用Spring Boot定義掃描範圍? 專案啟動時關於Compone

Spring學習spring註解自動注入bean

Spring mvc註解用到的配置: <!-- 啟用spring mvc 註解 --> <context:annotation-config /> <context:component-scan base-package

Spring使用註解配置bean

原理:      1、當啟動spring容器的時候,給spring容器的bean建立物件      2、當spring容器解析到<context:annotation-config></context:annotation-config>的時候,          spring容器掃描

@Autowired註解Spring入門

前一篇文章寫了在使用Spring框架過程中,通過在頭標籤中加入預設default-autowire來自動裝配,但是該方法有一定侷限性,就是需要被裝配類中有setter方法,且setter方法有一定要求

Spring Boot(33)、SpringBoot事務管理@Transactional註解原理

1、依賴包 1.1、 SpringBoot中的依賴包 眾所周知,在SpringBoot中凡是需要跟資料庫打交道的,基本上都要顯式或者隱式新增jdbc的依賴: <dependency> <groupId>org.springfram

SpringMVC@Controller@RequestMapping註解說明

一、@Controller註解 1、說明: 用於指示Spring類的例項是一個控制器,控制器可以支援同時處理多個請求動作。 2、保證Spring能找到控制器: (1)在SpringMVC的配置檔案的標頭檔案中引入spring-context (2)使用

Spring實戰Spring註解配置工作原理原始碼解析

一、背景知識在【Spring實戰】Spring容器初始化完成後執行初始化資料方法一文中說要分析其實現原理,於是就從原始碼中尋找答案,看原始碼容易跑偏,因此應當有個主線,或者帶著問題、目標去看,這樣才能最大限度的提升自身程式碼水平。由於上文中大部分都基於註解進行設定的(Spri

spring springmvcspringmvc使用註解宣告控制器與請求對映

# 概述 **註解:** 在Spring中儘管使用XML配置檔案可以實現Bean的裝配工作,但如果應用中Bean的數量較多,會導致XML配置檔案過於臃腫,從而給維護和升級帶來一定的困難。 從JDK 5開始提供了名為Annotation(註解)的功能,Spring正是利用這一特性,Spring逐步完善對Anno

Spring-Security1認證授權

部分 完整 業務 代碼 參數 web 用戶訪問 設置 管理權限 【認證】 憑據為基礎的認證: 當你登錄 e-mail 賬號時,你可能提供你的用戶名和密碼。E-mail的提供商會將你的用戶名與數據中的記錄進行匹配,並驗證你提供的密碼與對應的記錄是不是匹配。這些憑證(用戶名和

Hibernate學習 —— 抓取策略(註解方式)

屬性的方法 ould per hql 項目 操作記錄 新建 應用程序 span 當應用程序須要在關聯關系間進行導航的時候。hibernate怎樣獲取關聯對象的策略。 抓取策略的方式: FetchType.LAZY:懶載入。載入一個實體時。定

spring BootSpring中@Controller@RestController之間的區別

處理 public 不同 esp 舉例 rest control tro adding spring Boot入手的第一天,看到例子中的@RestController ............. 相同點:都是用來表示Spring某個類的是否可以接收HTTP請求 不同點:@C

spring Boot2.在Myecplise上把spring Boot項目打包 war包jar包

aps let failed htm 報錯 聲明 執行 spa oss ========================================================第一部分=========================================

spring cloudspring cloud Sleuth Zipkin 進行分散式鏈路跟蹤

spring cloud 分散式微服務架構下,所有請求都去找閘道器,對外返回也是統一的結果,或者成功,或者失敗。 但是如果失敗,那分散式系統之間的服務呼叫可能非常複雜,那麼要定位到發生錯誤的具體位置,就是一個比較麻煩的問題。 所以定位故障點,就引入了spring cloud Sleuth【Sleuth是獵

spring系列之6:bean的初始化銷燬方法

bean的生命週期:bean建立---初始化----銷燬的過程 容器管理bean的生命週期:我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期的時候來呼叫我們自定義的初始化和銷燬方法 構造(物件建立): 單例項:在容器啟動的時候建立物件 多例項:在每次獲

Feign中使用hystrix功能@EnableFeignClients@ComponentScan註解的一個坑

跟著尚矽谷的SpringCloud教程學的,然後在寫程式碼的時候用了比較新的版本去替代視訊裡的教學版本。 使用SpringCloud的Finchley.SR1版本 使用SpringBoot的2.0.1.RELEASE版本 在學到hystrix斷路器的時候 由於@Ena

Spring(七)用@Resource@Autowired註解完成屬性裝配及自動裝配

使用到註解需匯入jar包:common-annotations.jar 手工裝配依賴物件有兩種程式設計方式: 一、在xml配置檔案中通過bean節點進行配置,如: <?xml version="1.0" encoding="UTF-8"?>

spring父子容器關係@Value註解取不到值的問題

父子容器 Spring中可以包含多個容器,以SpringMVC為例, Spring為父容器 , SpringMVC為子容器 。 父容器中的bean對子容器的bean是可見的,但是子容器的b

Spring 系列1. 搭建配置Spring與jdbc整合的環境

配置資料來源 <!-- 配置結點,可以使用佔位符 --> <context:property-placeholder location=“classpath:jdbc.properties”/> <bean id="dataSource" class="org.apach