??? 存在不如意的地方就意味著存在bug。
?
??? 當我們像擺弄機器一般去利用這個那個框架的時候,很快,就陷入無休止的試驗,試驗這個那個的功能。創新簡直和我們絕緣。
??? 隨便抱怨一句。
??? 最近,公司同事利用spring+struts2做個工具,涉及到隱私,故我把代碼簡單化(普世的原則啊)。
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private FriendService service;
private Friend friend;
public FriendService getService() {
return service;
}
public void setService(FriendService service) {
this.service = service;
}
public Friend getFriend() {
return friend;
}
public void setFriend(Friend friend) {
this.friend = friend;
}
public String execute(){
System.out.println(service);
boolean result = service.check(friend);
if(result){
return SUCCESS;
// System.out.println(result);
}
return INPUT;
}
}
?
?
?? 這是一個action,非常俗氣的action,它有一個屬性service,很顯然接下來我們會利用spring的ioc方式來獲得service。下面列出本篇文章關鍵的部分,看好了
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dao1" class="com.nt.dao.impl.FriendDaoImpl">
</bean>
<bean id="service1" class="com.nt.service.impl.FriendServiceImpl">
<property name="dao" ref="dao1"></property>
</bean>
<bean id="login" class="com.nt.action.LoginAction">
<property name="service" ref="service1"></property>
</bean>
</beans>
?
??? 如果,稍加注意的話,大家會發現引用的FriendServiceImpl的id是service1。
?? 對,這就是問題所在,同事在debug模式下運行程序發現得到的service是 null。
??? 但是,他測試了許多篇,翻閱了什么李剛的書,還有springinaction之類,都沒發現問題的解決方式。
?
?? 后來,我建議,他把FriendServiceImpl的id改為service。
?? 問題終于解決!
?
??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

