#213 Prototype Bean Inside a Singleton Bean

Companies:
Sberbank
Themes:
SpringSpring CoreCode Review

What will be printed to the screen?

@Configuration
public class Test3 {

    @Component
    class SingletonBean {

        @Autowired
        PrototypeBean prototypeBean;

        public UUID getId() {
            return prototypeBean.getId();
        }
    }

    @Component
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    class PrototypeBean {

        private UUID id;

        @PostConstruct
        private void init() {
            id = UUID.randomUUID();
        }

        public UUID getId() {
            return id;
        }
    }

    public static void main(String[] args) {
        var context = new AnnotationConfigApplicationContext(Test3.class);
        var firstBean = context.getBean(SingletonBean.class);
        var secondBean = context.getBean(SingletonBean.class);

        System.out.println(firstBean.equals(secondBean));
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in