戴着假发的程序员出品
spring应用手册(第一部分)
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管理:
/** * @author 戴着假发的程序员 * * @description */ @Component public class Student{ }配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan use-default-filters="false" base-package="com.boxuewa.dk.demo5"> </context:component-scan> </beans>测试:
@Test public void testUseDefaultFilters(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-demo9.xml"); System.out.println(ac.getBean(Student.class)); }结果,抛出异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.boxuewa.dk.demo5.pk1.Student' available