这个问题已经被回答过好几次了,但我已经设置了所有注释,并且它似乎已正确连接。
用户服务接口(interface):
public interface UserService {
User findUserById(Long id);
User findUserByName(String name);
List<User> findAllUsers();
void saveUser(User user);
void updateUser(User user);
void deleteUserById(long id);
void deleteAllUsers();
public boolean isUserExist(User user);
}
用户服务Impl
@Service
@Transactional
public class UserServiceImpl implements UserService {
private static final AtomicLong counter = new AtomicLong();
private static List<User> users;
static {
users = populateDummyUsers();
}
@Override
public User findUserById(Long id) {
for (User user : users) {
if (user.getId() == id) {
return user;
}
}
return null;
}
@Override
public User findUserByName(String name) {
for (User user : users) {
if (user.getUsername().equalsIgnoreCase(name)) {
return user;
}
}
return null;
}
失败的测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringCrudApplication.class)
@ComponentScan("com.service")
public class ServiceTest {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Test
public void findUserByIdTest() throws Exception {
long id = (long) 0;
User userTest = new User(id,"Mike","644 Main St", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c9d8d8c4cde8cfc5c9c1c486cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>");
User user = userService.findUserById(id);
assert user.getAddress() != null;
assert user.getEmail() != null;
assert user.getUsername() != null;
}
我尝试在 com.service 和 com.serviceImpl 的测试类中使用 componentScan,但没有成功。
完整错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.service.test.ServiceTest':
Unsatisfied dependency expressed through method 'setUserService'
parameter 0: No qualifying bean of type [com.service.UserService]
found for dependency [com.service.UserService]: expected at least 1
bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {}; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [com.service.UserService] found for
dependency [com.service.UserService]: expected at least 1 bean which
qualifies as autowire candidate for this dependency. Dependency
annotations: {}
--------------------解决方案--------------------------------
我必须将 @ComponentScan 添加到主方法中:
@SpringBootApplication
@ComponentScan("com.serviceImpl, com.service")
public class SpringCrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCrudApplication.class, args);
}
}
最佳答案
ComnponentScan 必须找到实现,而不是接口(interface)。 因此,如果您的类 UserServiceImpl 位于包 com.serviceImpl 中,则必须扫描此包。
包名看起来很奇怪,包名只能是小写。 因此,请尝试将包重命名为 com.service.impl 并进行扫描。
因为您使用的是@Transactional,所以spring将创建一个实现UserService的代理。所以你只能注入(inject)UserService而不能注入(inject)UserServiceImpl。 检查您的代码,可能您正在尝试在其他地方@Autowire UserServiceImpl。
除了你的异常“没有匹配的类型的bean..”之外,堆栈跟踪中通常还会有一条类似“无法 Autowiring ”的消息,它准确地说明了, spring 尝试在哪里注入(inject)什么类型。
关于java - 没有找到类型匹配的 bean ... 来进行依赖...预计至少有 1 个有资格作为 Autowiring 候选者的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35901551/