当我启动我的应用程序时,我在stacktrace中收到此错误
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:
这是我在spring security 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));
}
}
我的控制器中的代码如下所示,使用了上面提到的Spring安全类
@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)}
请问我的代码显示上述错误
可能有什么问题答案 0 :(得分:0)
您是否已将@ComponentScan("com.your.securityservice.package")
添加到配置java / xml配置文件中?如果还没有,Spring容器将无法识别服务的注释。