Spring注解02 @ComponentScan 自动扫描组件

2019-01-05  本文已影响0人  運河的縴夫

前言

现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解、原理,比如@Conditional、@Import、@EnableXXX等。
如果掌握这些底层原理、注解,那么我们对这些高层框架就能做到高度定制,使用的游刃有余;

本文涵盖Spring的常用注解
@ComponentScan、@Bean、@Configuration、@Conditional、@Import、@PropertySource、@Profile等

1.@Configuration

@Configuration //告诉Spring这是一个配置类
public class MainConfig {
   //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
    @Bean(value = "person01")
    public Person person(){
        return new Person("jm",16,"tommy");
    }
}
public class MainTest {

    public static void main(String[] args) {
        final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        final Person person = applicationContext.getBean(Person.class);
        System.out.println("person = [" + person + "]");
        final String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            System.out.println(name);
        }
    }
}

person = [Person(name=jm, age=16, nickName=tommy)]
person01
Process finished with exit code 0

person01
给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id,也可以指定

2 @ComponentScans @ComponentScan

包扫描、只要标注了@Controller、@Service、@Repository,@Component

<context:component-scan base-package="com.tommy" use-default-filters="false"></context:component-scan>

@Configuration
@ComponentScan(value = "com.tommy")
public class MainConfig {
    //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
    @Bean(value = "person")
    public Person person01() {
        return new Person("jm", 16, "tommy");
    }
}

运行结果:
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
personController
personDao
personService
person

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

运行结果:
....省略....
mainConfig
personDao
person

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

运行结果:
.....省略.....
personController
personService
person

*注意:使用includeFilters 时,必须设置 useDefaultFilters = false,才能生效。

@Configuration
@ComponentScans(value = {
        @ComponentScan(value = "com.tommy",
                excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
                        classes = {Controller.class, Service.class})
                }, useDefaultFilters = false)
})

FilterType.ANNOTATION:按照注解
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:使用正则指定
FilterType.CUSTOM:使用自定义规则

@ComponentScan(value = "com.tommy",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        }, useDefaultFilters = false)

MyTypeFilter .java

public class MyTypeFilter implements TypeFilter {

    /**
     * @param metadataReader        读取到的当前正在扫描的类的信息
     * @param metadataReaderFactory 可以获取到其他任何类信息的
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        final AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        final ClassMetadata classMetadata = metadataReader.getClassMetadata();
        final Resource resource = metadataReader.getResource();

        final String className = classMetadata.getClassName();
        if (className.contains("er")) {
            System.out.println("--->" + className);
            return true;
        }
        return false;
    }
}

上一篇下一篇

猜你喜欢

热点阅读