spring boot应用程序启动,tomcat运行,然后在最终死亡之前引发错误。
运行我的应用程序给我这个Autowired错误:
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,但错误实际上有所不同:
3b9a6a2e
org.springframework.beans.factory.BeanCreationException:创建名称为’webSecurityConfig’的bean时出错:自动连接的依赖项注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService; 嵌套的异常是java.lang.IllegalArgumentException: 无法将myapp.MyUserDetailsService字段myapp.WebSecurityConfig.userDetailsService设置为com.sun.proxy。$ Proxy80
这使您的问题成为Spring无法创建bean)的副本。解决方案:更换
@Autowired private MyUserDetailsService userDetailsService;
与
@Autowired private UserDetailsService userDetailsService;
您的解决方案(删除@Transactional)之所以有效@Transactional,是因为没有理由,Spring不需要包装MyUserDetailsService代理。
@Transactional
MyUserDetailsService