#213 Prototype Bean Inside a Singleton Bean
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
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor