Announcement Announcement Module
Collapse
No announcement yet.
Does appContext.getBean("mybean") return new insta Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does appContext.getBean("mybean") return new insta

    Sorry, but I am a just getting into Spring....

    Does every call to appContext.getBean("mybean") return a new instance of the bean that I am fetching, or are all beans returned singletons within the JVM instance?

    If a bean is a singleton, I would expect
    appContext.getBean("mybean") == appContext.getBean("mybean")

    Thanks!

  • #2
    Re: Does appContext.getBean("mybean&quot return new i

    Originally posted by rhodepc
    Sorry, but I am a just getting into Spring....

    Does every call to appContext.getBean("mybean") return a new instance of the bean that I am fetching, or are all beans returned singletons within the JVM instance?
    It depends. If you make the bean a singleton (all bean in the application context are singleton by default) you will get the same bean from an application context. But if you have multiple applicationcontexts (this is rare) you will get different beans from different applicationcontexts.

    So
    Foo foo1 = (Foo)appContext.getBean("foo);
    Foo foo2 = (Foo)appContext.getBean("foo");
    foo1==foo2.

    And
    Foo foo1 = (Foo)appContext1.getBean("foo");
    Foo foo2 = (Foo)appContext2.getBean("foo");
    foo1!=foo2.

    You can check the documentation for more info

    There you can see how you can make a bean singleton (or not).

    Comment

    Working...
    X