spring boot应用程序启动,tomcat运行,然后在最终死亡之前引发错误。

错误

运行我的应用程序会给我这个Autowired错误:

2016-07-29 02:18:24.737 ERROR 44715 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [myapp.MyUserDetailsService] 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]

细节

我的安全性配置:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .antMatchers("/", "/register").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/")
        .permitAll()
        .and()
        .logout()
        .permitAll();
}

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
}

}

MyUserDetails:
@Service
@Transactional
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserRepository userRepository;


public MyUserDetailsService() {
        super();
}

@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
        User user = userRepository.findByUserName(userName);
        if (user == null) {
                return null;
        }
        List<GrantedAuthority> auth = AuthorityUtils
                                      .commaSeparatedStringToAuthorityList("ROLE_USER");
        String password = user.getPassword();
        return new org.springframework.security.core.userdetails.User(userName, password,
                                                                      auth);
}



}

您可以在Git Repo中找到完整的源代码。

最佳答案

我在commit 3b9a6a2e上尝试过您的项目,但错误实际上是不同的:

org.springframework.beans.factory.BeanCreationException:创建名称为'webSecurityConfig'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有myapp.MyUserDetailsS​​ervice myapp.WebSecurityConfig.userDetailsS​​ervice;嵌套异常为java.lang.IllegalArgumentException:无法将myapp.MyUserDetailsS​​ervice字段myapp.WebSecurityConfig.userDetailsS​​ervice设置为com.sun.proxy。$ Proxy80

这使您的问题成为Spring can't create beans的重复项。解决方案:更换

@Autowired
private MyUserDetailsService userDetailsService;


@Autowired
private UserDetailsService userDetailsService;

您的解决方案(删除@Transactional)之所以有效,是因为没有@Transactional,Spring没有理由将MyUserDetailsService包装在代理中。

08-28 06:15