1.32 context:component-scan的annotation-config属性

戴着假发的程序员出品

[查看视频教程]

annotation-config默认是true,完成了context:annotation-config元素的工作,如果是true就开启了属性自动注入的功能,如果是false就是关闭属性自动注入的功能。

案例:

我们创建两个类Person和Student,并且都交个spring管理,在Person中自动注入Student。

1 /**
2  * @author 戴着假发的程序员
3  *  
4  * @description
5  */
6 @Component
7 public class Student{
8 }
 1 /**
 2  * @author 戴着假发的程序员
 3  *  
 4  * @description
 5  */
 6 @Component
 7 public class Person {
 8     @Autowired
 9     private Student student;
10     public void shwoStu(){
11         System.out.println(student);
12     }
13 }

配置如下:

 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 annotation-config="false" base-package="com.boxuewa.dk.demo5">
11     </context:component-scan>
12 </beans>

测试:

1 @Test
2 public void testAnnotationConfig(){
3     ApplicationContext ac =
4             new ClassPathXmlApplicationContext("applicationContext-demo9.xml");
5     Person bean = ac.getBean(Person.class);
6     bean.shwoStu();
7 }

结果:

我们发现Person中的Stduent属性不能注入了。

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