戴着假发的程序员出品
context:component-scan的annotation-config属性
spring应用手册(第一部分)
annotation-config默认是true,完成了context:annotation-config元素的工作,如果是true就开启了属性自动注入的功能,如果是false就是关闭属性自动注入的功能。
案例:
我们创建两个类Person和Student,并且都交个spring管理,在Person中自动注入Student。
/**
* @author 戴着假发的程序员
*
* @description
*/
@Component
public class Student{
}
/**
* @author 戴着假发的程序员
*
* @description
*/
@Component
public class Person {
@Autowired
private Student student;
public void shwoStu(){
System.out.println(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 annotation-config="false" base-package="com.boxuewa.dk.demo5">
</context:component-scan>
</beans>
测试:
@Test
public void testAnnotationConfig(){
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext-demo9.xml");
Person bean = ac.getBean(Person.class);
bean.shwoStu();
}
结果:
我们发现Person中的Stduent属性不能注入了。