当前位置: 首页 > 知识库问答 >
问题:

创建名为“Registration Controller”的bean时出错:通过字段“Password Encoder”表示的不满足依赖项;

乐正宏深
2023-03-14

这是编译过程中出现的错误:

@Controller
@RequestMapping("/registration")
public class RegistrationController {

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private UserRepo userRepo;

@GetMapping
public String registration(){
    return "registration";
}

@PostMapping
public String processRegistration(RegistrationForm registrationForm){
    userRepo.save(registrationForm.toUser(passwordEncoder));
    return "redirect:/login";
}

}
@Configuration
@Autowired
private UserDetailsService userDetailsService;

// шифрование пароля
@Bean
public PasswordEncoder encoder(){
    return new BCryptPasswordEncoder(10);
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(encoder() );
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/","/home","login","registration").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login").defaultSuccessUrl("/").and()
            .logout().logoutUrl("/logout").logoutSuccessUrl("/").permitAll();

}
@NoRepositoryBean
public interface UserRepo extends CrudRepository<User,Long> {
  User findByUsername(String name);
}
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
 'registrationController': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inMemoryUserDetailsManager' defined in class path resource [org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 'inMemoryUserDetailsManager' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) [spring-boot-2.4.5.jar:2.4.5]
at com.security.Registration.RegistrationApplication.main(RegistrationApplication.java:10) [classes/:na]
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean 
  with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; 
  nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean 
  with name 'inMemoryUserDetailsManager' defined in class path resource 
  [org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.class]: 
  Bean instantiation via factory method failed; nested exception is 
  org.springframework.beans.BeanInstantiationException: Failed to instantiate 
  [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 
  'inMemoryUserDetailsManager' threw exception; nested exception is 
  org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 
   'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?
public class MyUserDetailsService implements UserDetailsService {

@Autowired
UserRepo userRepository;


@Override
public UserDetails loadUserByUsername(String username) throws 
  UsernameNotFoundException {
    return userRepository.findByUsername(username);
  }
}

共有1个答案

洪伟兆
2023-03-14

丹尼斯。stacktrace的最后一行表示:

    org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 
   'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

这意味着,当您试图在configure方法中使用“encoder”bean时,“encoder”bean处于创建阶段。类似的问题在本题中已经得到了解决。我认为您应该像这些问题的答案所建议的那样做--使encoder()方法静态化。

 类似资料: