2- @ComponentScan註解

1. @ComponentScan 註解

在配置類中只要標註了@ComponentScan 註解,Spring就可以自動掃描Value對應的包了

1.1. 配置類

package com.spring.config;

import com.spring.beans.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(value = "com.spring")
public class MainConfig {

    @Bean
    public Person person() {
        return new Person("fj",26);
    }
}

以上掃描com.spring包中的所有組件

1.2 單元測試類

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest_1 {
    @SuppressWarnings("resource")
    @Test
    public void test01() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
		// 獲取容器中的所有組件
        String[] names = applicationContext.getBeanDefinitionNames();
        for(String name: names)
            System.out.println(name);
    }
}

輸出的結果

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
bookController
bookService
bookDao
person

前面五個都是Spring自己的組件,其中mainConfig類是因爲增加了@Configuration註解,bookController類是因爲增加了@Controller註解,bookService類是因爲增加了@Servide註解,bookDao類是因爲增加了@Repository註解,pereson類是因爲在mainConfig類的person方法中增加了@Bean註解

1.3 excludeFilters 排除

package com.spring.config;

import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(value = "com.spring", excludeFilters = {
        @ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
})
public class MainConfig {

    @Bean
    public Person person() {
        return new Person("fj",26);
    }
}

根據註解類型進行排除

1.4 includeFilters 僅包含

package com.spring.config;

import com.spring.beans.Person;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(value = "com.spring", includeFilters = {
        @ComponentScan.Filter(type= FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class MainConfig {

    @Bean
    public Person person() {
        return new Person("fj",26);
    }
}

使用僅包含includeFilters需要禁用默認的過濾規則,即將useDefaultFilters設置爲false

1.5 FilterType.CUSTOM自定義過濾規則


import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class MyTypeFilter implements TypeFilter {

    @Override
    // MetadataReader: 讀取到的當前正在掃描的類的信息
    // MetadataReaderFactory: 可以獲取到其他任何類的信息
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        // 獲取當前類註解的信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 獲取到當前正在掃描的類的類信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 獲取當前類資源(類的路徑)
        Resource resource = metadataReader.getResource();

        String className = classMetadata.getClassName();
        System.out.println("類名:" + className);

        if(className.contains("dao"))
            return true;

        return false;
    }
}

總結

  1. excludeFilers = Filter[], 指定掃描的時候按照什麼規則排除哪些組件
  2. includeFilers = Filter[], 指定掃描的時候按照什麼規則包含哪些組件
  3. FilterType.ANNOTATION, 按照註解
  4. FilterType.ASSIGNABLE_TYPE,按照給定的類型
  5. FilterType.ASPECTJ,使用ASPECTJ表達式
  6. FilterType.REGEX,使用正則規則
  7. FilterType.CUSTOM,使用自定義規則
  8. @ComponentScan註解還可以疊加使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章