新人刚入职不久,今天遇到个问题超级困惑
若有大神能帮忙解惑,悬赏50个大洋
急急急!
项目用的是spring+mybatis+struts框架
在action注入service的set方法中
public void setBuildingInfoService(BuildingInfoService buildingInfoService) {
this.buildingInfoService = buildingInfoService;
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}
System.out.println("buildingInfoService注入成功");
}
控制台输出注入成功,这就说明注入成功,
但是到了下面execute方法执行的时候
@Override
public String execute() throws Exception {
//获取请求的建筑名主键参数
String building_name = ServletActionContext.getRequest().getParameter("building_name");
//此处继续测试service,发现此时service已为null
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}
System.out.println("buildingInfoService注入成功");
//调用service层找到该建筑对象 产生空指针异常
BuildingInfoDataBean bd = buildingInfoService.getBuildingByBuildingName(building_name);
在执行方法里,service变成了null,百思不得其解,非常困惑,望大神解答!在线等
问了同事,同事说我配置文件有问题,但是具体又找不到
如果是配置文件问题的话service对象会注入成功吗
要不试试定义一个全局的变量来接收注入的service
有道理啊 ,我试试,谢谢大哥给我一个思路
你把buildingInfoService打印出来看看。。
BuildingInfoService接口
public interface BuildingInfoService {
/**
* 根据建筑名获取该建筑对象
* @param building_name String-建筑名
* @return BuildingInfoDataBean:建筑对象 null:未查询到数据
*/
public BuildingInfoDataBean getBuildingByBuildingName(String building_name);
}
BuildingInfoServiceImpl实现类
public class BuildingInfoServiceImpl implements BuildingInfoService{
private BuildingInfoDao buildingInfoDao = null;
public BuildingInfoDao getBuildingInfoDao() {
return buildingInfoDao;
}
public void setBuildingInfoDao(BuildingInfoDao buildingInfoDao) {
this.buildingInfoDao = buildingInfoDao;
}
@Override
public BuildingInfoDataBean getBuildingByBuildingName(String building_name) {
return buildingInfoDao.getBuildingByBuildingName(building_name);
/* 测试
* return JDBCGetBuildingInfoDao.getBuildingInfo(building_name);*/
}
}
Spring相关配置
<!-- 建筑基本信息 -->
<bean id="BuildingInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xayr.salary.dao.BuildingInfoDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="BuildingInfoService" class="com.xayr.salary.serviceImpl.BuildingInfoServiceImpl">
<property name="BuildingInfoDao">
<ref bean="BuildingInfoDao" />
</property>
</bean>
<bean id="BuildingInfoAction" class="com.xayr.salary.action.BuildingInfoAction">
<property name="BuildingInfoService">
<ref bean="BuildingInfoService"/>
</property>
</bean>
打印出来的确有对象,如下图
@自私的程序员: ,,,不是这个意思,
我意思是你把buildingInfoService值打印出来,然后一点点向外推总能发现问题所在,既然你有这样的疑问,就应该去看看到底是哪个原因
或者 debug一下
@代码飞了: 恩是的,打印出来确实有对象地址,但是到了下面方法里就没了,就是null
public void setBuildingInfoService(BuildingInfoService buildingInfoService) {
this.buildingInfoService = buildingInfoService;
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}
System.out.println("buildingInfoService注入成功");
}
控制台输出注入成功,这就说明注入成功
if没有else
加进去了,没作用的,兄弟
@自私的程序员:
//此处继续测试service,发现此时service已为null
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}
你这里为空的话还不进行处理
继续执行这个肯定空指针啊
buildingInfoService.getBuildingByBuildingName(building_name);
和这个无关吧,空的实例无法调用方法啊
/* 测试
* return JDBCGetBuildingInfoDao.getBuildingInfo(building_name);*/
这里的实参调用的是实现BuildingInfoService接口的实例这个BuildingInfoServiceImpl类的对象
吧,看看这个对象是不是就是空的
public void setBuildingInfoService(BuildingInfoService buildingInfoService) {
this.buildingInfoService = buildingInfoService;
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}
System.out.println("buildingInfoService注入成功");
}
是不是没有写构造方法啊。
BuildingInfoService接口
public interface BuildingInfoService {
/**
* 根据建筑名获取该建筑对象
* @param building_name String-建筑名
* @return BuildingInfoDataBean:建筑对象 null:未查询到数据
*/
public BuildingInfoDataBean getBuildingByBuildingName(String building_name);
}
BuildingInfoServiceImpl实现类
public class BuildingInfoServiceImpl implements BuildingInfoService{
private BuildingInfoDao buildingInfoDao = null;
public BuildingInfoDao getBuildingInfoDao() {
return buildingInfoDao;
}
public void setBuildingInfoDao(BuildingInfoDao buildingInfoDao) {
this.buildingInfoDao = buildingInfoDao;
}
@Override
public BuildingInfoDataBean getBuildingByBuildingName(String building_name) {
return buildingInfoDao.getBuildingByBuildingName(building_name);
/* 测试
* return JDBCGetBuildingInfoDao.getBuildingInfo(building_name);*/
}
}
Spring相关配置
<!-- 建筑基本信息 -->
<bean id="BuildingInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xayr.salary.dao.BuildingInfoDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="BuildingInfoService" class="com.xayr.salary.serviceImpl.BuildingInfoServiceImpl">
<property name="BuildingInfoDao">
<ref bean="BuildingInfoDao" />
</property>
</bean>
<bean id="BuildingInfoAction" class="com.xayr.salary.action.BuildingInfoAction">
<property name="BuildingInfoService">
<ref bean="BuildingInfoService"/>
</property>
</bean>