| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3065 人关注过本帖
标题:Spring配置注入service成功,但是调用方法报空指针
只看楼主 加入收藏
kingkingwd
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-8-5
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
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>
搜索更多相关主题的帖子: service public action null 大洋 
2016-08-05 16:55
losememory
Rank: 2
等 级:论坛游民
帖 子:1
专家分:20
注 册:2016-8-2
收藏
得分:20 
你把<property name=  命名成小写  
2016-08-05 18:01
吃货丶
Rank: 2
等 级:论坛游民
帖 子:14
专家分:41
注 册:2016-8-24
收藏
得分:0 
<property name="">里面的东西以小写开头
2016-08-24 17:45
快速回复:Spring配置注入service成功,但是调用方法报空指针
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011521 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved