一、@CompentScan
组件注册-@ComponentScan-自动扫描组件&指定扫描规则
1)在xml文件配置的方式,我们可以这样来进行配置:
<!-- 包扫描、只要标注了@Controller、@Service、@Repository,@Component --> <context:component-scan base-package="cn.liuliang"/>
2)以前是在xml配置文件里面写包扫描,现在我们可以在配置类里面写包扫描:
@Configuration //声明该类是配置类 @ComponentScan("cn.liuliang.studysoundcodespring") //包扫描 public class SpringConfig { /** * 向容器中主入一个bean组件 * @return */ @Bean public User user(){ // 通过new创建对象,放入容器中 return new User("j3-liuliang",28); } }
3)结果
从上面的测试结果我们可以发现主配置类 SpringConfig也是IOC容器里面的组件,也被纳入了IOC容器的管理:
我们从@Configuration
这个注解点进去就可以发现这个注解上也标注了 @Component
的这个注解,也纳入到IOC容器中作为一个组件:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { /** * Explicitly specify the name of the Spring bean definition associated * with this Configuration class. If left unspecified (the common case), * a bean name will be automatically generated. * <p>The custom name applies only if the Configuration class is picked up via * component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}. * If the Configuration class is registered as a traditional XML bean definition, * the name/id of the bean element will take precedence. * @return the specified bean name, if any * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator */ String value() default ""; }
我们在 @ComponentScan
这个注解上,也是可以指定要排除哪些包或者是只包含哪些包来进行管理:里面传是一个Filter[]数组
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { // 忽略其他源代码 /** * Indicates whether automatic detection of classes annotated with {@code @Component} * {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled. */ // 默认扫描规则,默认值true boolean useDefaultFilters() default true; /** * Specifies which types are eligible for component scanning. * <p>Further narrows the set of candidate components from everything in {@link #basePackages} * to everything in the base packages that matches the given filter or filters. * <p>Note that these filters will be applied in addition to the default filters, if specified. * Any type under the specified base packages which matches a given filter will be included, * even if it does not match the default filters (i.e. is not annotated with {@code @Component}). * @see #resourcePattern() * @see #useDefaultFilters() */ // 只扫描指定路径包,不过要将默认扫描规则关闭 Filter[] includeFilters() default {}; /** * Specifies which types are not eligible for component scanning. * @see #resourcePattern */ // 排除一些包不扫描 Filter[] excludeFilters() default {}; }
1.1 excludeFilters
那接下来我们可以事实如下操作:
excludeFilters:排除某某不扫描
@Configuration //声明该类是配置类 //@ComponentScan("cn.liuliang.studysoundcodespring") //包扫描 @ComponentScan(value = "cn.liuliang.studysoundcodespring",excludeFilters = { //这里面是一个@Filter注解数组,FilterType.ANNOTATION表示的排除的规则 :按照注解的方式来进行排除 //classes = {Controller.class,Service.class}表示的是标有这些注解的类给排除掉 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class}) }) public class SpringConfig { /** * 向容器中主入一个bean组件 * @return */ @Bean public User user(){ // 通过new创建对象,放入容器中 return new User("j3-liuliang",28); } }
1.2 includeFilters
includeFilters:指定在扫描的时候,只需要包含哪些组件
在用xml文件配置的方式来进行配置的时候,还要禁用掉默认的配置规则,只包含哪些组件的配置才能生效
<context:component-scan base-package=“cn.liuliang.springannotationdemo” use-default-filters=“false”/>
@Configuration //声明该类是配置类 //@ComponentScan("cn.liuliang.studysoundcodespring") //包扫描 @ComponentScan(value = "cn.liuliang.studysoundcodespring",includeFilters = { //这里面是一个@Filter注解数组,FilterType.ANNOTATION表示的排除的规则 :按照注解的方式来进行扫描 //classes = {Repository.class}表示的是只扫描标有这些注解的类 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Repository.class}) // 禁用掉默认的配置规则 },useDefaultFilters = false) public class SpringConfig { /** * 向容器中主入一个bean组件 * @return */ @Bean public User user(){ // 通过new创建对象,放入容器中 return new User("j3-liuliang",28); } }
@ComponentScan
这个注解是可以重复定义的:来指定不同的扫描策略
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) //可重复定义 public @interface ComponentScan { }
那我们可以如下配置:但是这样写的话,就必须要java8及以上的支持
@Configuration //声明该类是配置类 //@ComponentScan("cn.liuliang.studysoundcodespring") //包扫描,如下两个扫描配置相当于这一个配置 @ComponentScan(value = "cn.liuliang.studysoundcodespring",includeFilters = { //这里面是一个@Filter注解数组,FilterType.ANNOTATION表示的排除的规则 :按照注解的方式来进行扫描 //classes = {Repository.class}表示的是只扫描标有这些注解的类 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Repository.class}) },useDefaultFilters = false) @ComponentScan(value = "cn.liuliang.studysoundcodespring",excludeFilters = { //这里面是一个@Filter注解数组,FilterType.ANNOTATION表示的排除的规则 :按照注解的方式来进行扫描 //classes = {Repository.class}表示的是不扫描标有这些注解的类 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Repository.class}) }) public class SpringConfig { /** * 向容器中主入一个bean组件 * @return */ @Bean public User user(){ // 通过new创建对象,放入容器中 return new User("j3-liuliang",28); } }
二、 ComponentScans
我们还可以用 @ComponentScans
来定义多个扫描规则:里面是@ComponentScan
规则的数组
//告诉Spring这是一个配置类 @Configuration @ComponentScans( value = { @ComponentScan(value = "cn.liuliang.springannotationdemo",includeFilters = { //这里面是一个@Filter注解数组,FilterType.ANNOTATION表示的排除的规则 :按照注解的方式来进行扫描 //classes = {Controller.class}表示的是只扫描标有这些注解的类 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}) },useDefaultFilters = false) ,@ComponentScan(value = "cn.liuliang.springannotationdemo") } ) public class PersionConfig { }
2.1 FilterType
组件注册-自定义TypeFilter指定过滤规则
我们可以来看看有哪几种过滤规则:
public enum FilterType { // 按照注解类型 比如@Controller @Service @Repository @Compent ANNOTATION, // 按照给定的类型来进行匹配 ASSIGNABLE_TYPE, // 表达式来进行匹配,这个不常用 ASPECTJ, // 按照正则表达式的方式来进行匹配 REGEX, // 我们可以自己来写一个匹配规则的类:MyTypeFilter,这个类要实现TypeFilter这个接口 CUSTOM }
我们来说说最后一种:自定义匹配规则FilterType.CUSTOM
我们可以自己来写一个匹配规则的类:MyTypeFilter,这个类要实现TypeFilter这个接口
public class MyTypeFilter implements TypeFilter { /** * * @param metadataReader the metadata reader for the target class 读取到当前正在扫描的类的信息 * @param metadataReaderFactory a factory for obtaining metadata readers 可以获取到其他任何类的信息 * @return * @throws IOException */ @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //获取到当前类注解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //获取当前类的资源的信息(比如类的路径) Resource resource = metadataReader.getResource(); //获取到当前正在扫描的类的信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); String className = classMetadata.getClassName(); System.out.println("通过自定义的匹配规则--->"+className); // false表示不扫描该组件 return false; } }
这个时候,我们就可以这样来用了:使用FilterType.CUSTOM
//告诉Spring这是一个配置类 @Configuration @ComponentScan(value = "cn.liuliang.springannotationdemo",includeFilters = { //自定义匹配的规则 @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class}), },useDefaultFilters = false) public class SpringConfig { }
结束语
- 由于博主才疏学浅,难免会有纰漏,假如你发现了错误或偏见的地方,还望留言给我指出来,我会对其加以修正。
- 如果你觉得文章还不错,你的转发、分享、点赞、留言就是对我最大的鼓励。
- 感谢您的阅读,十分欢迎并感谢您的关注。