AI Hints
Home›Spring Boot Tutorial›@Configuration and @Bean, Bean Scopes and Practice: Layers
Spring Boot · Lesson 35 of 120

Bean Scopes

One or many.

⏱ 2 min read▶ Video at 45:53🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

We said Spring creates one of each bean. That's the default. But you can ask for a new object, every time one is needed.

  • One of each: the default
  • Or a new one
  • Every time

Common scopes

Singleton is the default. There's one object, shared by everyone. Prototype creates a new object each time it's injected. And in web apps, request scope gives one per request. Most beans should stay singletons.

ScopeHow many objects
singleton (default)one, shared
prototypea new one per injection
requestone per HTTP request

Two beans, two scopes

Basket is a normal component, so it's a singleton. Receipt has at Scope prototype. Both keep a list, and add returns the whole list.

Shop.javajava
@Component
class Basket {
    private final List<String> items = new ArrayList<>();
    public List<String> add(String item) { items.add(item); return items; }
}

@Component
@Scope("prototype")
class Receipt {
    private final List<String> lines = new ArrayList<>();
    public List<String> add(String line) { lines.add(line); return lines; }
}

Shared or separate

The controller asks for two baskets, and two receipts. We add Dune using basket A, and receipt A. Then we add Circe using basket B, and receipt B. Run it. Basket B already holds Dune, because A and B are the same object. But receipt B only holds Circe. Each receipt was a new object.

ShopController.javajava
@RestController
public class ShopController {

    private final Basket basketA, basketB;
    private final Receipt receiptA, receiptB;

    public ShopController(Basket a, Basket b, Receipt c, Receipt d) {
        basketA = a; basketB = b; receiptA = c; receiptB = d;
    }

    @GetMapping("/demo")
    public String demo() {
        basketA.add("Dune");
        receiptA.add("Dune");
        return "basket B: " + basketB.add("Circe")
            + "\nreceipt B: " + receiptB.add("Circe");
    }
}
Output
basket B: [Dune, Circe]
receipt B: [Circe]
Also uses Shop.java
@Component
class Basket {
    private final List<String> items = new ArrayList<>();
    public List<String> add(String item) { items.add(item); return items; }
}

@Component
@Scope("prototype")
class Receipt {
    private final List<String> lines = new ArrayList<>();
    public List<String> add(String line) { lines.add(line); return lines; }
}
Also uses _curl.sh
curl localhost:8080/demo

A warning about state

Singletons are shared, by every request, at the same time. So a singleton should not keep data that changes, like the basket here. That would mix up different users. Keep services stateless, and keep data in a database.

keyvalue
Singletonsshared by all requests
Changing fieldsdangerous
Serviceskeep them stateless
Databelongs in a database

Summary

Beans are singletons by default, shared by everyone. Prototype makes a new one each time. Keep singletons stateless. Next, some practice.

Key takeaways
  • singleton (default)
  • prototype
  • Stay stateless
Next up: Practice: Layers — your turn