Spring启动应用程序启动,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
找到完整的源代码答案 0 :(得分:6)
我已经在提交3b9a6a2e
尝试了您的项目,错误实际上是不同的:
org.springframework.beans.factory.BeanCreationException:创建名称为&#39; webSecurityConfig&#39;的注册:自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private myapp.MyUserDetailsService myapp.WebSecurityConfig.userDetailsService;嵌套异常是java.lang.IllegalArgumentException:无法将myapp.MyUserDetailsService字段myapp.WebSecurityConfig.userDetailsService设置为com.sun.proxy。$ Proxy80
这使您的问题与Spring can't create beans重复。解决方案:替换
@Autowired
private MyUserDetailsService userDetailsService;
与
@Autowired
private UserDetailsService userDetailsService;
您的解决方案(删除@Transactional
)有效,因为没有@Transactional
,Spring没有理由将MyUserDetailsService
包裹在代理中。
答案 1 :(得分:2)
从@Transactional
移除MyUserDetailsService
。不知道为什么。如果有人有解释,请发帖,我会将其标记为答案。
仔细检查,这确实是正确的,如果您想自行检查,请查看上面列出的存储库的提交840f0753bdca1592d9070c74bc87f30e8e2f87a7
,并进行测试。
并停止低估这个答案,因为你认为是不正确的。
答案 2 :(得分:1)
请添加注释:[HockeySDK] WARNING: Detecting crashes is NOT enabled due to running the app with a debugger attached.
在主配置类上。
如果要为特定目录创建bean / service,那么可以在@ComponentScan("")