Исключение BeanCreation при запуске jar-файла SpringBoot - PullRequest
0 голосов
/ 10 марта 2020

Я создаю приложение Spirng Securited с некоторым кодировщиком паролей и учетными записями пользователей. У меня проблема при запуске файла .jar. Когда я запускаю его в IDE, все отлично работает. Похоже, создание Бина с именем passwordEncoder должно быть завершено до создания userService. Я не знаю, как заставить такое создание. Или, может быть, я ошибаюсь, и проблема в другом. Вот код:

Bean:

  @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }

UserService

 @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public UserService(UserRepository userRepository,
                       RoleRepository roleRepository,
                       BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.userRepository = userRepository;
        this.roleRepository = roleRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

SecurityConfig

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

Exceprion

 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'instalator': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in URL [jar:file:/home/suchar/Dokumenty/Java%20Training/JDict/target/JDictionary-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/pl/micsoc/dictionary/service/UserService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'bCryptPasswordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'passwordEncoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

Ответы [ 2 ]

0 голосов
/ 10 марта 2020

В UserService, bean-компонент BCryptPasswordEncoder автоматически подключается дважды. Вот что вы получаете исключение. Вставьте зависимость либо через переменную класса, либо через конструктор, но не делайте это обоими способами. Либо частный BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
public UserService(UserRepository userRepository,
                   RoleRepository roleRepository,
                   BCryptPasswordEncoder bCryptPasswordEncoder) {
    this.userRepository = userRepository;
    this.roleRepository = roleRepository;
    this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

или

@ Автоматически подключенный частный BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
public UserService(UserRepository userRepository,
                   RoleRepository roleRepository) {
    this.userRepository = userRepository;
    this.roleRepository = roleRepository;
}
0 голосов
/ 10 марта 2020

попробуйте с этим в UserService

private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
public UserService(UserRepository userRepository,
                   RoleRepository roleRepository,
                   @Lazy BCryptPasswordEncoder bCryptPasswordEncoder) {
    this.userRepository = userRepository;
    this.roleRepository = roleRepository;
    this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...