一、【注解】Spring注解@ComponentScan

@ComponentScan作用是将标注了@Controller、@Service、@Repository等注解的类放入IOC容器中,统一管理。

一、基本用法

Component组件:

@Controller
public class UserController {

}
@Service
public class UserService {

}
@Repository
public class UserDao {

}

配置类:

@Configuration
@ComponentScan(value = "com.zhangjianbing.spring.component")
public class UserConfig {

}

测试:

public class MainTest {
    @Test
    public void m1() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();// Bean的定义信息
        for (String beanName : beanDefinitionNames) {
            System.out.println("======" + beanName);
        }
    }
}
测试结果:
======org.springframework.context.annotation.internalConfigurationAnnotationProcessor
======org.springframework.context.annotation.internalAutowiredAnnotationProcessor
======org.springframework.context.annotation.internalRequiredAnnotationProcessor
======org.springframework.context.annotation.internalCommonAnnotationProcessor
======org.springframework.context.event.internalEventListenerProcessor
======org.springframework.context.event.internalEventListenerFactory
======userConfig
======userController
======userDao
======userService

二、Filter过滤

排除

@Configuration
// 排除UserController类,其它的都要
@ComponentScan(
    value = "com.zhangjianbing.spring.component", 
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {UserController.class})})
public class UserConfig {

}

只包含

// 只包含(需要禁用掉默认的过滤规则)
@ComponentScan(
    value = "com.zhangjianbing.spring.component", 
    useDefaultFilters = false, 
    includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {UserController.class})})
public class UserConfig {

}

自定义Filter

自定义Filter实现TypeFilter接口

/**
 * 自定义实现扫描类型过滤器{@link FilterType}
 *
 * @author zhangjianbing
 * time 2020/09/23
 * https://www.zhangjianbing.com
 */
public class MyFilterType implements TypeFilter {

    /**
     * @param metadataReader        读取当前正在扫描的类的信息
     * @param metadataReaderFactory 获取其它任何类的信息
     */
    @Override
    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();
        if (className.contains("con")) {// 将类名包含er的注册到容器中
            return true;
        }
        return false;
    }
}

配置文件:

@Configuration
@ComponentScan(
        value = "com.zhangjianbing.spring.component",
        useDefaultFilters = false,
        includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyFilterType.class})}
        )
public class UserConfig {

}

测试结果:

public class MainTest {
    @Test
    public void m1() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();// Bean的定义信息
        for (String beanName : beanDefinitionNames) {
            System.out.println("======" + beanName);
        }
    }
}
测试结果:
======org.springframework.context.annotation.internalConfigurationAnnotationProcessor
======org.springframework.context.annotation.internalAutowiredAnnotationProcessor
======org.springframework.context.annotation.internalRequiredAnnotationProcessor
======org.springframework.context.annotation.internalCommonAnnotationProcessor
======org.springframework.context.event.internalEventListenerProcessor
======org.springframework.context.event.internalEventListenerFactory
======userConfig
======userController
posted @ 2020-09-23 08:22  在谷歌上百度  阅读(188)  评论(0编辑  收藏  举报