Card 02/ 07
All 7 cards
ExampleDifficulty: Intermediate1 min
Prototype Scope: a Fresh Instance Every Time
ReceiptBuilder is declared prototype scope instead of the default. Two calls to getBean on it behave nothing like the same call on a singleton.
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
ReceiptBuilder receiptBuilder() { return new ReceiptBuilder(); }ReceiptBuilder a = context.getBean(ReceiptBuilder.class);
ReceiptBuilder b = context.getBean(ReceiptBuilder.class);
System.out.println("same instance: " + (a == b));same instance: falseEvery getBean call on a prototype produces a new object. That is the whole trade prototype scope makes: no sharing, and — as the next card shows — no help from the container once it's handed over, either.