Melodly 2017-04-26 10:27 采纳率: 0%
浏览 4491

Service为什么为什么注入为空

1.首先我的Dao层注入成功,已经测试成功,但是Controller成注入Service时,发现注入的Service为空。
2.配置文件

2.1 web.xml

.....

contextConfigLocation

classpath:application-context.xml

<!-- spring的监听器 -->

org.springframework.web.context.ContextLoaderListener

......

2.2 application-context.xml

2.3 config文件下xml配置文件

****anotation.xml

<!--spring 扫包 @Service .....-->

/context:component-scan

context:annotation-config/

        其余配置文件为Dao层数据库相关,以及sql映射文件的配置,就不写了

2.4 Springmvc的配置文件

<!-- springmvc扫包 -->

/context:component-scan

<!-- 配置全局日期转换器 -->

<!-- jsp视图解析器 -->

测试类

@Autowired
BrandDao brandDao;
BrandService brandService;
@Test
public void test3() throws Exception{
    Brand brand = new Brand();
    brand.setIsDisplay(1);
    //当前页码(如果当前页码小于1或者为空时,将其置为1)
    brand.setPageNo(1);
    System.out.println(brandService);
    Pagination pagination=brandService.getBrandListWithPage(brand);
    }
}

Service实现类
@Service
@Transactional
public class BrandServiceImpl {
@Autowired
BrandDao brandDao;

@Transactional(readOnly = true)
public Pagination getBrandListWithPage(Brand brand) {
    /*
     * @pageNo:当前页码
     * 
     * @pageSize:每页的记录数
     * 
     * @totalCount:总记录数
     */
    System.out.println(brandDao);
    System.out.println(brandDao.getBrandCount(brand));
    int pageNo=brand.getPageNo();

    Pagination pagination=new Pagination(Pagination.cpn(pageNo), 5, brandDao.getBrandCount(brand));
    pagination.setList(brandDao.getBrandListWithPage(brand));
    return pagination;
}

}

  • 写回答

1条回答 默认 最新

  • 夕阳雨晴 2017-04-26 10:46
    关注

    你的service中有没有添加@Service或者@Component注解呀

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集