2016-06-10 30 views
0

當我啓動我的應用我在堆棧跟蹤嵌套的例外是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)} 

請問我的代碼顯示上述錯誤會出現什麼問題

+0

SecurityServiceImpl沒有用'@ Component'(或'@ Service')註釋,所以它不是Spring bean。 –

+0

其實我在我的代碼中,@Service public class UserServiceImpl implements UserService { – Blaze

+0

錯誤隱藏在您未發佈的內容中:正在掃描的軟件包是什麼?什麼是你的實現包。它是否擴展了正確的界面? –

回答

0

您是否已將@ComponentScan("com.your.securityservice.package")添加到您的配置java/xml配置文件中?如果你還沒有,Spring容器將不會識別該服務的註釋。

+0

我擁有它...... – Blaze

+0

你可以發佈完整的堆棧跟蹤嗎?用你提供的東西,沒有什麼可能會出錯。 – AlexOlsen

+0

我在控制器類中註釋掉了Autowired,它工作正常。這是合理的嗎>>>>>>>>>> // @ Autowired private SecurityService securityService; – Blaze

相關問題