1.31 context:component-scan的use-default-filters属性

戴着假发的程序员出品

[查看视频教程]

context:component-scan是用来通知spring自动扫描指定的包中的类文件的。

use-default-filters属性是用来通知spring是否启用默认的Filter。这个配置默认是true,spring的默认Filter就会处理@Component、@Controller、@Service、@Repository这些注解的Bean。

如果use-default-filters配置为false,则spring就不会再扫描和处理上面这些注解的Bean。

案例:

我们准备一个类Student,交给sprin管理:

1 /**
2  * @author 戴着假发的程序员
3  *  
4  * @description
5  */
6 @Component
7 public class Student{
8 }

配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:component-scan  use-default-filters="false" base-package="com.boxuewa.dk.demo5">
11     </context:component-scan>
12 </beans>

测试:

1 @Test
2 public void testUseDefaultFilters(){
3     ApplicationContext ac =
4             new ClassPathXmlApplicationContext("applicationContext-demo9.xml");
5     System.out.println(ac.getBean(Student.class));
6 }

结果,抛出异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.boxuewa.dk.demo5.pk1.Student' available

 

posted @ 2020-10-06 09:04  戴着假发的程序员0-1  阅读(196)  评论(0编辑  收藏  举报