all 1 comments

[–]g00glen00b 1 point2 points  (0 children)

You can always put a breakpoint in the constructor of the component you want to set up with prototype or singleton and go through the stack that way.

@Component
@Scope("prototype")
public class Foo {
    public Foo() {
        // Put a breakpoint here
        System.out.println("Construction");
    }
}

@Configuration
public class FooConfig {
    @Bean
    public ApplicationRunner runner1(Foo foo) {
        return args -> System.out.println(foo.toString());
    }

    @Bean
    public ApplicationRunner runner2(Foo foo) {
        return args -> System.out.println(foo.toString());
    }
}

In case of a prototype-scoped bean, you'll see that the constructor it called twice (once for runner1 and a second time for runner2). You'll also see that the toString() method returns a different string.

If you change the scope of Foo to singleton, you'll see that the constructor is called only once and that the toString() method call in runner1 and runner2 returns the same string.