SpringMvc+mybatis 无法注入service的bean

时间:2022-10-27 13:10:37
错误如下
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);
}


试边百度各种方法均不行 求哪位大神指教

16 个解决方案

#1


我也碰到这类问题,同问 ?????求大神 ....  SpringMvc+mybatis 无法注入service的beanSpringMvc+mybatis 无法注入service的bean

#2


先在mybatis的config中批量扫描,然后mybatis和spring整合包有个MapperScannerConfigurer,在spring的配置文件中配置,然后用bean配置service接口。

#3


applicationContext.xml配置文件中只扫描除开controller层的包,在springmvc配置文件中只扫描controller层。

#4


我把serviceimpl包放进去问题解决了,但总感觉哪里不对

#5


@Repository("userService ")加上这个
public class UserServiceImpl implements UserService 

#6


在你service实现类前面加上@Service("userService")  

#7


<context:component-scan base-package="com.ssm.controller"/>
你的service也在这个包下?如果没有的话,就是没有扫描service,也就没办法注入了

#8



感觉配置有点乱

#9


我也遇到了相应的问题,看评论说是扫描包的问题,因为com.ssm.controller 只会扫描controller,但是我如果改为com.ssm呢?我的所有包都在ssm下。是否可以扫描到?   

#10


同样的问题,求解决办法;我一直很好奇,网上好多spring+springmvc+mybatis的整合也是这样配置的都可以运行,为何我们的不能

#11


我遇到了同样的问题,搞了一天都没解决,呼叫大牛帮忙看一下

#12


一样的问题 懵B了已经 2天了

#13


一样的问题,已经好多天了,呼唤大牛!!!

#14


我也遇到一样的问题,请问楼主这个问题解决了没有?

#15


在spring的配置文件中添加service自动扫描的包:
<context:component-scan base-package="com.xlx.service.*" />
但是这样的结局是service无法找到dao层的bean

#16


同问啊!!!!!

#1


我也碰到这类问题,同问 ?????求大神 ....  SpringMvc+mybatis 无法注入service的beanSpringMvc+mybatis 无法注入service的bean

#2


先在mybatis的config中批量扫描,然后mybatis和spring整合包有个MapperScannerConfigurer,在spring的配置文件中配置,然后用bean配置service接口。

#3


applicationContext.xml配置文件中只扫描除开controller层的包,在springmvc配置文件中只扫描controller层。

#4


我把serviceimpl包放进去问题解决了,但总感觉哪里不对

#5


@Repository("userService ")加上这个
public class UserServiceImpl implements UserService 

#6


在你service实现类前面加上@Service("userService")  

#7


<context:component-scan base-package="com.ssm.controller"/>
你的service也在这个包下?如果没有的话,就是没有扫描service,也就没办法注入了

#8



感觉配置有点乱

#9


我也遇到了相应的问题,看评论说是扫描包的问题,因为com.ssm.controller 只会扫描controller,但是我如果改为com.ssm呢?我的所有包都在ssm下。是否可以扫描到?   

#10


同样的问题,求解决办法;我一直很好奇,网上好多spring+springmvc+mybatis的整合也是这样配置的都可以运行,为何我们的不能

#11


我遇到了同样的问题,搞了一天都没解决,呼叫大牛帮忙看一下

#12


一样的问题 懵B了已经 2天了

#13


一样的问题,已经好多天了,呼唤大牛!!!

#14


我也遇到一样的问题,请问楼主这个问题解决了没有?

#15


在spring的配置文件中添加service自动扫描的包:
<context:component-scan base-package="com.xlx.service.*" />
但是这样的结局是service无法找到dao层的bean

#16


同问啊!!!!!