标题
添加@Resource后。No beans of ‘IUserService’ type found。。这个问题捏 就是在spring-mybatis.xml中加个注解。。。
impl包应在service中,以便扫描
<context:component-scan base-package="com.qfedu.service" />
错误日志
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.qfesu.test.TestService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.qfedu.service.IUserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
org.springframework.beans.factory.BeanCreationException:创建名为“com.qfesu.test.TestService”的bean时出错:资源依赖项注入失败;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有’com.qfedu.service.IUserService’类型的限定bean可用:至少需要一个符合autowire候选条件的bean。依赖项批注:{@javax.annotation.Resource(shareable=true,lookup=,name=,description=,authenticationType=CONTAINER,type=class java.lang.Object,mappedName=)}
3.测试代码
package com.qfesu.test;
import com.qfedu.pojo.User;
import com.qfedu.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
public class TestService {
@Autowired
private IUserService userService;
@Test
public void testGetAllUser(){
List<User> list = userService.getAllUsers();
for (User user : list) {
System.out.println(user);
}
}
}
是因为植入依赖失败 没有关于srervice包的扫描,在spring-mybatis.xml 也要写
最好在spring-mvc 与mybatis中都写 ,这样页面与测试就不会报错了。
现在就OK了!!
HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
这个问题也被我解决了