当我启动我的应用我在堆栈跟踪嵌套的例外是NoSuchBeanDefinitionException:无类型的匹配豆[com.springfoundation.service.SecurityService]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.springfoundation.service.SecurityService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
得到这个错误,这是我在我的春天代码安全class.java
public interface SecurityService {
String findLoggedInUsername();
void autologin(String username, String password);
}
这是我的界面实施者的代码
public class SecurityServiceImpl implements SecurityService {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
private static final Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);
@Override
public String findLoggedInUsername() {
Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails();
if (userDetails instanceof UserDetails) {
return ((UserDetails)userDetails).getUsername();
}
return null;
}
@Override
public void autologin(String username, String password) {
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
authenticationManager.authenticate(usernamePasswordAuthenticationToken);
if (usernamePasswordAuthenticationToken.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
logger.debug(String.format("Auto login %s successfully!", username));
}
}
在我的位指示的代码如下所示利用春节安全I级以上
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
说我使用上述声明映射仍然控制器类
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
这是内部的我目前面临的问题的完整堆栈跟踪
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private
com.springfoundation.service.SecurityService com.springfoundation.controller.UserController.securityService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type
[com.springfoundation.service.SecurityService] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513) ~[spring-beans-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92) ~[spring-beans-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ~[spring-beans-3.1.4.RELEASE.jar:3.1.4.RELEASE]
... 22 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.springfoundation.service.SecurityService] 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)}
请问我的代码显示上述错误会出现什么问题
SecurityServiceImpl没有用'@ Component'(或'@ Service')注释,所以它不是Spring bean。 –
其实我在我的代码中,@Service public class UserServiceImpl implements UserService { – Blaze
错误隐藏在您未发布的内容中:正在扫描的软件包是什么?什么是你的实现包。它是否扩展了正确的界面? –