码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-10-06 20:35:53      阅读:30      评论:0      收藏:0      [点我收藏+]

标签:code   技术   print   utf-8   encoding   not   ase   关闭   can   

戴着假发的程序员出品

[查看视频教程]

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属性不能注入了。

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

标签:code   技术   print   utf-8   encoding   not   ase   关闭   can   

原文地址:https://www.cnblogs.com/jiafa/p/13772629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!