1.@Component注解与其衍生注解

(1) 在Spring中,@Component注解用于说明某个类是一个bean,之后Spring在类路径扫描过程中会将该bean添加至容器中;@Component注解还有很多衍生注解,如@Repository, @Service和@Controller,它们分别用于三层架构中的持久层,业务层和控制层,因此,对于一个业务层的普通Service类,一般情况下用@Component或@Service都是可行的,但更推荐用@Service注解,因为该注解不仅能清晰的指明被标注的类是一个业务类,此外,这些注解还是Spring AOP的理想切入目标,能方便的对某一层进行切入(例如:切入控制层,进行权限校验)

(2) Spring提供的许多注解都可以用作元注解,如Spring的@RestController注解,它就是将@Controller和@ResponseBody注解组合出来的一个新注解,使用该注解,在我们期望类中所有方法都返回json格式数据时,就不用在每一个方法上都标注@ResponseBody注解了,因此,Spring推荐我们根据业务需要,结合元注解来实现自定义注解

2.自动扫描并注册bean

(1) 上文我们提到了@Component注解等,那么,Spring该如何扫描到这些注解并将它们注入进容器中呢? 这就需要类路径扫描注解@ComponentScan了,我们需要把它添加到有@Configuration注解标注的配置类上,在容器启动后,Spring就会扫描指定包下的bean并将其注入进容器中,如下所示

//声明3个类,这三个类均位于cn.example.spring.boke同一个包下,其中ExampleA和ExampleB被注解标注,表明它们是bean,需要被注入进容器中,ExampleC是一个普通的类@Servicepublic class ExampleA { }@Componentpublic class ExampleB { }public class ExampleC { }//使用@Configuration注解定义一个配置类,在该配置类上,使用@ComponentScan注解指定包扫描路径为cn.example.spring.boke,这时,Spring就会扫描该包以及该包的子包下所有符合条件的bean,并将它们注入容器中,该注解等同于在之前提到过的标签@Configuration@ComponentScan("cn.example.spring.boke")public class Config { }//启动容器,注意这里使用的是AnnotationConfigApplicationContext容器,而非之前的ClassPathXmlApplicationContext容器,而我们也不再需要xml配置文件,实现了完全的基于注解的配置AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);//打印结果如下,可以看到,容器扫描到了exampleA和exampleB以及我们的配置类config,而忽略了exampleC(其他的bean是Spring隐式自动添加的)org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactoryconfigexampleAexampleB

3.使用过滤器来自定义组件扫描规则

(1) 默认情况下,Spring仅检测被@Component、@Repository、@Service、@Controller、@Configuration和以@Component等作为元注解的自定义注解(如:@RestController)标注的类,不过,我们可以通过自定义@ComponentScan注解中的过滤器属性includeFilters和excludeFilters来修改这一默认行为,如下

//只修改上面例子中的配置类,此处假设我们只扫描被@Service注解标注的类,其自定义过滤规则如下//includeFilters:要被注入的    excludeFilters:不要被注入的  @ComponentScan.Filter:指定过滤类型@Configuration@ComponentScan(basePackages = "cn.example.spring.boke",                includeFilters = @ComponentScan.Filter(Component.class),                excludeFilters = @ComponentScan.Filter(Service.class))public class Config {}//输出结果如下,可见我们的exampleA已被排除在外org.springframework.context.annotation.internalConfigurationAnnotationProcessororg.springframework.context.annotation.internalAutowiredAnnotationProcessororg.springframework.context.annotation.internalCommonAnnotationProcessororg.springframework.context.event.internalEventListenerProcessororg.springframework.context.event.internalEventListenerFactoryconfigexampleBexampleC

过滤类型:

过滤类型说明
annotation(默认,按照注解过滤)如上面例子中的属性excludeFilters = @ComponentScan.Filter(Service.class),就是排除掉所有被@Service注解标注的类
assignable( 按照类型过滤)例如:excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ExampleA.class),就是排除掉ExampleA这个类型的bean
aspectj(按照切面表达式过滤)例如:cn.example..*Service+,这是一个切入点表达式,会对所有符合这个切入点表达式的bean进行过滤
regex(按照正则表达式过滤)同上,只不过满足的是正则表达式
custom(自定义过滤器)实现org.springframework.core.type.TypeFilter接口来自定义过滤器,实现自定义过滤规则

(2) 设置@ComponentScan注解中的useDefaultFilters属性值为false,来禁用默认过滤器,即关闭Spring对于那些默认注解(如@Component等)的自动扫描检测

4.定义组件的配置元数据

未完待续…