1.首先,你需要对applicationContext.xml文件加入
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
加入以上红字部分才可以使用标签:<context:component-scan base-package="com.annotation.document"></context:component-scan>
在这个标签里面的 base-package属性的值是给spring在这个包主包,往下的子包中搜索有注解@Component的类进行初始化,并且该注解默认使用构造函数进行初始化