Spring配置注入service成功,但是调用方法报空指针
新人刚入职不久,今天遇到个问题超级困惑若有大神能帮忙解惑,悬赏50个大洋
项目用的是spring+mybatis+struts框架
在action注入service的set方法中
public void setBuildingInfoService(BuildingInfoService buildingInfoService) {
this.buildingInfoService = buildingInfoService;
if(buildingInfoService==null){
System.out.println("buildingInfoService注入失败");
}else{
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注入失败");
}else{
System.out.println("buildingInfoService注入成功");
System.out.println(buildingInfoService);
}
//调用service层找到该建筑对象 产生空指针异常
BuildingInfoDataBean bd = buildingInfoService.getBuildingByBuildingName(building_name);
在执行方法里,service变成了null,百思不得其解,非常困惑,望大神解答!在线等
问了同事,同事说我配置文件有问题,但是具体又找不到
如果是配置文件问题的话service对象会注入成功吗
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>