Spring's component scans comment

Via class annotated @ComponentScan Spring scanning component class.

@ComponentScan optional parameters

  • basePackages: specify the root directory of the package to be scanned, its subdirectories are also scanned, the default path is @ComponentScan annotation category directory and its subdirectories
  • value: basePackages alias
  • basePackageClasses: Specifies the root directory of the class to be scanned, its subdirectories will be scanned
  • lazyInit: whether lazy loading, default false
  • useDefaultFilters: whether to enable automatic scanning component, the default true; false represents only apply filtering rules specified below
  • excludeFilters: Specifies the need to scan the component type (see @Filter configuration)
  • includeFilters: Specifies the type of component to scan (see @Filter configuration)
  • scopedProxy: proxy mode ScopedProxyMode
    • DEFAULT: By default, the general is NO
    • NO: no need to create a proxy
    • INTERFACES: Create a JDK proxy
    • TARGET_CLASS: Create a proxy using the CGLIB
  • nameGenerator: Specifies BeanNameGenerator implements an interface class can be used to define the generation rule BeanName
  • scopeResolver: Specifies AnnotationScopeMetadataResolver class implements an interface can be configured scope scope, and proxy mode scopedProxy
  • resourcePattern: suitable components to match, the default is ** / * class, and recommended includeFilters excludeFilters.

A plurality of methods @ComponentScan

Here are three ways

@ComponentScans(
    @ComponentScan({"com.xxx.aaa","com.xxx.bbb"})
)
@Configuration
public class Config {}
@ComponentScan({"com.xxx.aaa","com.xxx.bbb"})
@Configuration
public class Config {}
@ComponentScan({"com.xxx.aaa"})
@ComponentScan({"com.xxx.bbb"})
@Configuration
public class Config {}

Configure a filter @Filter

1, the filter class annotation @Service

@Configuration
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.ANNOTATION, classes = Service.class)
    }
)
public class Config {}

2, the filter class Dog

@Configuration
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Dog.class)
    }
)
public class Config {}

3, the regular matching, a class suffix filter

@Configuration
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.REGEX, pattern = {"..*a"})
    }
)
public class Config {}

4, custom filtering rules-based filtering of a type comprising

@Configuration
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = MyFilterType.class)
    }
)
public class Config {}
// 实现 TypeFilter 自定义规则:过滤包含 a 的 BeanName
public class MyFilterType implements TypeFilter 自定义规则 {
  @Override
  public boolean match(MetadataReader reader, MetadataReaderFactory factory) throws IOException {
    return reader.getClassMetadata().getClassName().contains("a");
  }
}

one example

Class configuration Config, the following rules:

  1. Packet need to scan path com.xxx.aaa,com.xxx.bbb
  2. Excluded com.xxx.aaain the path @Serviceannotation classes and Dogclass
  3. com.xxx.bbbDo not enable automatic scanning, but need to include Catclasses
@ComponentScan(
    value = {"com.xxx.aaa"},
    excludeFilters = {
        @Filter(type = FilterType.ANNOTATION, classes = Service.class),
        @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Dog.class)
    }
)
@ComponentScan(
    value = {"com.xxx.bbb"},
    useDefaultFilters = false,
    includeFilters = {
        @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = Cat.class)
    }
)
@Configuration
public class Config {
}

Guess you like

Origin www.cnblogs.com/bigshark/p/11286298.html