<bean id="xardDao"
class="dao.impl.xardDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<bean id="xardService"
class="service.impl.xardServiceImpl">
<property name="xardDao">
<ref bean="xardDao" />
</property>
</bean>
现在想往一个普通类Test里注入xardService,
<bean id="Test"
class="util.Text">
<property name="xardService">
<ref bean="xardService" />
</property>
</bean>
这时候就在Test中报空指针异常了,显然注入没有成功,set方法也写了。
请教怎么往一个普通类里注入。这个类不是接口,没关系的吧。
从日志看出,服务器一启动的时候就帮我把applicationContext加载了
2009-05-13 12:15
提问者采纳
没有注入成功是因为命名需合乎sun规范,而且配置文件中必须和实际类中的变量名要满足一致的。
如:
<bean id="xardService"
class="service.impl.xardServiceImpl">
<property name="xardDao">
<ref bean="xardDao" />
</property>
</bean>
那你的xardServiceImpl.java中必须有这样一个成员:
****
xardDaoImpl xardDao;
****
而且要有get,set方法。
那相似的,你的Text.java中必须有成员:
xardServiceImpl xardService;
提问者评价
-
大哥
评论
4
1
2009-05-13 11:58
80705041
六级
2009-05-13 12:39
liyi830813
八级
从你发的部分来看好像没什么问题
最好是把错误提示发上来,检查有没有重复的bean,service中是否存在xardDao并且已经有get set方法(这个最好是用软件自动生成的)
评论
0
0
2009-05-13 03:33
user1temp
三级
不要接口一样可以.
有set方法,加上你的这段配置是没问题的.
问题是你要确定运行test类的时候,是否执行加载了applicationContext配置文件.
<bean id="xardDao" class="dao.impl.xardDaoImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate" /> </property> </bean> <bean id="xardService" class="service.impl.xardServiceImpl"> <property name="xardDao"> <ref bean="xardDao" /> </property> </bean> 现在想往一个普通类Test里注入xardService, <bean id="Test" class="util.Text"> <property name="xardService"> <ref bean="xardService" /> </property> </bean> 这时候就在Test中报空指针异常了,显然注入没有成功,set方法也写了。 请教怎么往一个普通类里注入。这个类不是接口,没关系的吧。
从日志看出,服务器一启动的时候就帮我把applicationContext加载了
2009-05-13 12:15
提问者采纳
没有注入成功是因为命名需合乎sun规范,而且配置文件中必须和实际类中的变量名要满足一致的。 如: <bean id="xardService" class="service.impl.xardServiceImpl"> <property name="xardDao"> <ref bean="xardDao" /> </property> </bean> 那你的xardServiceImpl.java中必须有这样一个成员: **** xardDaoImpl xardDao; **** 而且要有get,set方法。 那相似的,你的Text.java中必须有成员: xardServiceImpl xardService;
-
提问者评价
-
大哥
评论
4
1
2009-05-13 11:58
80705041
六级
2009-05-13 12:39
liyi830813
八级
从你发的部分来看好像没什么问题
最好是把错误提示发上来,检查有没有重复的bean,service中是否存在xardDao并且已经有get set方法(这个最好是用软件自动生成的)
最好是把错误提示发上来,检查有没有重复的bean,service中是否存在xardDao并且已经有get set方法(这个最好是用软件自动生成的)
评论
0
0
2009-05-13 03:33
user1temp
三级
不要接口一样可以.
有set方法,加上你的这段配置是没问题的.
问题是你要确定运行test类的时候,是否执行加载了applicationContext配置文件.
有set方法,加上你的这段配置是没问题的.
问题是你要确定运行test类的时候,是否执行加载了applicationContext配置文件.