存在不如意的地方就意味着存在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。
问题终于解决!

