SpringMvc+mybatis 无法注入service的bean

s550716540 2015-12-31 05:03:58
错误如下
Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ssm.service.UserService com.ssm.controller.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ssm.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)

spring-mvc.xml配置

<context:component-scan base-package="com.ssm.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

controller 代码

@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public String toIndex(HttpServletRequest request,Model model){
int userId = Integer.parseInt(request.getParameter("id"));
User user = this.userService.getUserById(userId);
model.addAttribute("user", user);
return "index";
}
}



service接口

public interface UserService {
public User getUserById(int id);
}


service实现

@Service
public class UserServiceImpl implements UserService {
@Autowired
private IUserDao userDao;
@Override
public User getUserById(int id) {
// TODO Auto-generated method stub
return this.userDao.getUserById(id);
}

}


dao接口

public interface IUserDao {
public User getUserById(int id);
}


试边百度各种方法均不行 求哪位大神指教
...全文
2864 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
In_new 2017-12-07
  • 打赏
  • 举报
回复
同问啊!!!!!
JohnaXu 2017-09-26
  • 打赏
  • 举报
回复
在spring的配置文件中添加service自动扫描的包: <context:component-scan base-package="com.xlx.service.*" /> 但是这样的结局是service无法找到dao层的bean
liuxianzhidx 2016-08-15
  • 打赏
  • 举报
回复
我也遇到一样的问题,请问楼主这个问题解决了没有?
kobolo 2016-07-29
  • 打赏
  • 举报
回复
一样的问题,已经好多天了,呼唤大牛!!!
qq_23848575 2016-07-20
  • 打赏
  • 举报
回复
一样的问题 懵B了已经 2天了
xiaoluhaohao 2016-04-11
  • 打赏
  • 举报
回复
我遇到了同样的问题,搞了一天都没解决,呼叫大牛帮忙看一下
Alyces 2016-04-11
  • 打赏
  • 举报
回复
同样的问题,求解决办法;我一直很好奇,网上好多spring+springmvc+mybatis的整合也是这样配置的都可以运行,为何我们的不能
Smal1_Sun 2016-03-23
  • 打赏
  • 举报
回复
我也遇到了相应的问题,看评论说是扫描包的问题,因为com.ssm.controller 只会扫描controller,但是我如果改为com.ssm呢?我的所有包都在ssm下。是否可以扫描到?
wushen333 2016-01-07
  • 打赏
  • 举报
回复
<context:component-scan base-package="com.ssm.controller"/> 你的service也在这个包下?如果没有的话,就是没有扫描service,也就没办法注入了
心随自在飞 2016-01-07
  • 打赏
  • 举报
回复
感觉配置有点乱
clarezzx 2016-01-06
  • 打赏
  • 举报
回复
在你service实现类前面加上@Service("userService")
懒懒程序员 2016-01-05
  • 打赏
  • 举报
回复
@Repository("userService ")加上这个 public class UserServiceImpl implements UserService
s550716540 2016-01-05
  • 打赏
  • 举报
回复
我把serviceimpl包放进去问题解决了,但总感觉哪里不对
hersing 2016-01-04
  • 打赏
  • 举报
回复
applicationContext.xml配置文件中只扫描除开controller层的包,在springmvc配置文件中只扫描controller层。
zhoulychn 2016-01-04
  • 打赏
  • 举报
回复
先在mybatis的config中批量扫描,然后mybatis和spring整合包有个MapperScannerConfigurer,在spring的配置文件中配置,然后用bean配置service接口。
qq_32872907 2016-01-04
  • 打赏
  • 举报
回复
我也碰到这类问题,同问 ?????求大神 ....

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧