3

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);
}

}

我的用户详细信息:

@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找到完整的源代码

4

4 回答 4

6

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

org.springframework.beans.factory.BeanCreationException:创建名为“webSecurityConfig”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 myapp.MyUserDetailsS​​ervice myapp.WebSecurityConfig.userDetailsS​​ervice;嵌套异常是 java.lang.IllegalArgumentException: Can not set myapp.MyUserDetailsS​​ervice field myapp.WebSecurityConfig.userDetailsS​​ervice to com.sun.proxy.$Proxy80

这使您的问题与Spring can't create beans重复。解决方法:更换

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
private UserDetailsService userDetailsService;

您的解决方案(删除@Transactional)有效,因为没有@TransactionalSpring 没有理由包装MyUserDetailsService在代理中。

于 2016-07-29T09:38:58.160 回答
2

@TransactionalMyUserDetailsService工作中移除。不知道为什么。如果有人有解释,请发布,我会将其标记为答案。

仔细检查,这确实是正确的,如果您想自己检查,请检查840f0753bdca1592d9070c74bc87f30e8e2f87a7上面列出的存储库的提交,并对其进行测试。

并停止对这个答案投反对票,因为您认为它不正确。

于 2016-07-28T21:58:33.470 回答
1

@ComponentScan("") 请在主配置类上添加注释:

如果您想为特定目录创建 bean/service,那么您可以在里面提供包路径@ComponentScan("package.to.scan")

于 2016-07-29T05:48:43.153 回答
1

添加@ComponentScan到 WebSecurityConfig。

由于组件扫描,这应该在 MyUserDetailsS​​ervice 中连接,因为它用@Service.

可以将包名作为参数传递给注解,以设置组件扫描的范围,例如:

@ComponentScan("com.company.team")
于 2016-07-28T21:36:19.557 回答