Spring 學習二 @ComponentScan 元件掃描
阿新 • • 發佈:2018-12-22
IOC 容器預設掃描 @Controller,@Service,@Repository,@Component 註解的元件
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { /** 是否使用預設規則 */ boolean useDefaultFilters() default true; /** 包含掃描的元件 */ Filter[] includeFilters() default {}; /** 不掃描的元件 */ Filter[] excludeFilters() default {}; }
@Retention(RetentionPolicy.RUNTIME)
@Target({})
@interface Filter {
/** 過濾規則,按什麼型別過濾 */
FilterType type() default FilterType.ANNOTATION;
/** 需要過濾的元件 */
@AliasFor("classes")
Class<?>[] value() default {};
}
@Configuration @ComponentScan(value = "com.john", excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) }) public class BeanConfig { // 說明:排除帶 @Controller 註解的元件,不掃描 }
@Configuration @ComponentScan(value = "com.john", includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) }, useDefaultFilters = false) public class BeanConfig { // 說明:只掃描com.john 包下面的 帶@Controller 註解的元件,使用 includeFilters,需要禁用預設規則,即設定 useDefaultFilters = false }