Ami*_*ani 2 java spring spring-ioc spring-boot
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]
Run Code Online (Sandbox Code Playgroud)
我的安全配置:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在Git Repo找到完整的源代码
我在提交时尝试过你的项目,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;
Run Code Online (Sandbox Code Playgroud)
同
@Autowired
private UserDetailsService userDetailsService;
Run Code Online (Sandbox Code Playgroud)
您的解决方案(删除@Transactional)有效,因为没有@Transactional任何理由将Spring包装MyUserDetailsService在代理中.
| 归档时间: |
|
| 查看次数: |
7528 次 |
| 最近记录: |