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;
}
}