单个注入
@Import({
CacheClientConfiguration.class, MongoTemplateGetter.class, AwsMongoMigrationConfig.class, MultiMongoConfiguration.class
})
扫描
@ComponentScan(basePackages = {
"com.service.impl"
}, excludeFilters = @ComponentScan.Filter(
type = FilterType.CUSTOM,
classes = CustomFilter.class
))
其中FilterType有以下几种
public enum FilterType {
/**
* Filter candidates marked with a given annotation.
* @see org.springframework.core.type.filter.AnnotationTypeFilter
*/
ANNOTATION,
/**
* Filter candidates assignable to a given type.
* @see org.springframework.core.type.filter.AssignableTypeFilter
*/
ASSIGNABLE_TYPE,
/**
* Filter candidates matching a given AspectJ type pattern expression.
* @see org.springframework.core.type.filter.AspectJTypeFilter
*/
ASPECTJ,
/**
* Filter candidates matching a given regex pattern.
* @see org.springframework.core.type.filter.RegexPatternTypeFilter
*/
REGEX,
/** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM
}
排除包含固定注解的类
public class KafkaListenersTypeExcludeFilter extends TypeExcludeFilter {
private static final String KAFKA_LISTENER = "org.springframework.kafka.annotation.KafkaListener";
private static final String KAFKA_HANDLER = "org.springframework.kafka.annotation.KafkaHandler";
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
return annotationMetadata.hasAnnotatedMethods(KAFKA_LISTENER)
|| annotationMetadata.hasAnnotatedMethods(KAFKA_HANDLER);
}
@Override
public boolean equals(Object o) {
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return 1;
}
}
注入MockBean(用于单元测试,无需要调用的Bean)
@MockBean({})