SpringMvc+mybatis 无法注入service的bean

J2EE 码拜 9年前 (2016-02-02) 1747次浏览
错误如下
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);
}

试边百度各种方法均不行 求哪位高手指点

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

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SpringMvc+mybatis 无法注入service的bean
喜欢 (0)
[1034331897@qq.com]
分享 (0)