今天在以前的项目中加入spring依赖注入,来管理零散的类,发生了一个错误,弄了一上午,终于发现了问题所在。
applicationContext-bean.xml配置文件片段:
问题:在SecInputAction类中调用ruleProxy,报空指针错误。
原因:SecInputAction是我在程序中new出来的,不是从spring配置文件中取出来的,所以ruleProxy为空。
解决:首先getBean("secInputAction"),然后再取ruleProxy就不会是空了。
总结:只有在spring容器中取得的bean,它的属性才会被注入,否则无效。
applicationContext-bean.xml配置文件片段:
<bean name="secInputAction" class="com.sunyard.rule.action.SecInputAction">
<property name="ruleProxy" ref="ruleProxy" />
</bean>
问题:在SecInputAction类中调用ruleProxy,报空指针错误。
原因:SecInputAction是我在程序中new出来的,不是从spring配置文件中取出来的,所以ruleProxy为空。
解决:首先getBean("secInputAction"),然后再取ruleProxy就不会是空了。
总结:只有在spring容器中取得的bean,它的属性才会被注入,否则无效。