spring负责依赖注入,给项目开发带来很多方便性。但是今天一不小心发现通过注解的方式来实现属性对象注入失败,得到值为null。经过检查,发现注解的写法没错,失败另有原因。
最后发现,applicationContext.xml文件中少了
<context:component-scan base-package="/" />完整的是这样的:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <span style="color:#ff0000;"><context:component-scan base-package="/" /></span>
<span style="white-space:pre"> </span><bean>.......</bean>
<beans>