N
N
Nightmare0582021-12-06 02:07:57
Java
Nightmare058, 2021-12-06 02:07:57

Why is there a cyclical addiction?

Good evening.
I am making a REST API based on the following tutorial
https://habr.com/en/post/482552/

And for some reason a cyclic dependency appeared
Specifically, this moment was made exactly as in the article
61ad458eafe45539318553.png

I tried to add the Lazy annotation, it gives an error
61ad460fcaa05103891377.png

Here is a link to the git,
Help please sort out the reasons
https://gitlab.com/alkella99/notes_api/-/tree/master

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2021-12-06
@xez

@Configuration
public class AppConfig {

    @Autowired
    @Lazy
    UserService userService;

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

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder());
    }
}

@Configuration is not a bean, not a service. You don't need to autowire anything there.
@Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder());
    }
- there is some kind of abracadabra here. What does void do in config? Why is he throwing an exception?
If you need to perform some actions after the bean is built, think about the @PostConstruct annotation.
You can try, for example, add to the UserService:
...
    @Autowired
    AuthenticationManagerBuilder auth;

  @PostConstruct
    void init() {
        auth.userDetailsService(this).passwordEncoder(bCryptPasswordEncoder);
    }
...

N
Nightmare058, 2021-12-09
@Nightmare058

I solved it all by specifying in application.properties:
spring.main.allow-circular-references: true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question