What is scope of Spring Class annoted using @component annotation in spring mvc?












1















I was going through spring tutorials and was looking at default scope of beans , so by default scope is singletone only.
But when we use annotation in classes what scope they have ? do they create single object only for those class in JVM ? if yes than how web application works will it be thread safe ?










share|improve this question























  • They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

    – JB Nizet
    Jan 2 at 18:00


















1















I was going through spring tutorials and was looking at default scope of beans , so by default scope is singletone only.
But when we use annotation in classes what scope they have ? do they create single object only for those class in JVM ? if yes than how web application works will it be thread safe ?










share|improve this question























  • They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

    – JB Nizet
    Jan 2 at 18:00
















1












1








1








I was going through spring tutorials and was looking at default scope of beans , so by default scope is singletone only.
But when we use annotation in classes what scope they have ? do they create single object only for those class in JVM ? if yes than how web application works will it be thread safe ?










share|improve this question














I was going through spring tutorials and was looking at default scope of beans , so by default scope is singletone only.
But when we use annotation in classes what scope they have ? do they create single object only for those class in JVM ? if yes than how web application works will it be thread safe ?







spring spring-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 17:58









arjunarjun

1351113




1351113













  • They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

    – JB Nizet
    Jan 2 at 18:00





















  • They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

    – JB Nizet
    Jan 2 at 18:00



















They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

– JB Nizet
Jan 2 at 18:00







They're singletons. They're thread-safe because they typically are stateless, i.e. their only instance variables are references to other beans that are initialized at startup and never modified. Of course, if you don't know what you're doing and store mutable state in these singletons, they won't be thread-safe.

– JB Nizet
Jan 2 at 18:00














2 Answers
2






active

oldest

votes


















2















But when we use annotation in classes what scope they have?




They are singletons unless you use @Scope and specify a different scope.




Do they create a single object only for those class in JVM?




Spring creates one object per container. It's important since your JVM can run several Spring containers at once.




Will a web application be thread safe?




It's up to you. Spring can guarantee that lifecycle operations over a component are performed in a thread-safe manner (e.g. a bean instance is published thread-safely). However, Spring can't predict your application logic and how you define its correctness. For this reason, it doesn't provide any level of synchronisation, which might be insufficient or an overhead.



A good discussion on this part is here.






share|improve this answer

































    0














    Every bean managed by Spring container has Singleton scope by default no matter either you use annotation or xml, unless you don't override its default one.






    share|improve this answer
























    • it's nice, but it partly answers the question

      – Andrew Tobilko
      Jan 3 at 9:10












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011005%2fwhat-is-scope-of-spring-class-annoted-using-component-annotation-in-spring-mvc%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    But when we use annotation in classes what scope they have?




    They are singletons unless you use @Scope and specify a different scope.




    Do they create a single object only for those class in JVM?




    Spring creates one object per container. It's important since your JVM can run several Spring containers at once.




    Will a web application be thread safe?




    It's up to you. Spring can guarantee that lifecycle operations over a component are performed in a thread-safe manner (e.g. a bean instance is published thread-safely). However, Spring can't predict your application logic and how you define its correctness. For this reason, it doesn't provide any level of synchronisation, which might be insufficient or an overhead.



    A good discussion on this part is here.






    share|improve this answer






























      2















      But when we use annotation in classes what scope they have?




      They are singletons unless you use @Scope and specify a different scope.




      Do they create a single object only for those class in JVM?




      Spring creates one object per container. It's important since your JVM can run several Spring containers at once.




      Will a web application be thread safe?




      It's up to you. Spring can guarantee that lifecycle operations over a component are performed in a thread-safe manner (e.g. a bean instance is published thread-safely). However, Spring can't predict your application logic and how you define its correctness. For this reason, it doesn't provide any level of synchronisation, which might be insufficient or an overhead.



      A good discussion on this part is here.






      share|improve this answer




























        2












        2








        2








        But when we use annotation in classes what scope they have?




        They are singletons unless you use @Scope and specify a different scope.




        Do they create a single object only for those class in JVM?




        Spring creates one object per container. It's important since your JVM can run several Spring containers at once.




        Will a web application be thread safe?




        It's up to you. Spring can guarantee that lifecycle operations over a component are performed in a thread-safe manner (e.g. a bean instance is published thread-safely). However, Spring can't predict your application logic and how you define its correctness. For this reason, it doesn't provide any level of synchronisation, which might be insufficient or an overhead.



        A good discussion on this part is here.






        share|improve this answer
















        But when we use annotation in classes what scope they have?




        They are singletons unless you use @Scope and specify a different scope.




        Do they create a single object only for those class in JVM?




        Spring creates one object per container. It's important since your JVM can run several Spring containers at once.




        Will a web application be thread safe?




        It's up to you. Spring can guarantee that lifecycle operations over a component are performed in a thread-safe manner (e.g. a bean instance is published thread-safely). However, Spring can't predict your application logic and how you define its correctness. For this reason, it doesn't provide any level of synchronisation, which might be insufficient or an overhead.



        A good discussion on this part is here.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 18:47

























        answered Jan 2 at 18:07









        Andrew TobilkoAndrew Tobilko

        28.4k104589




        28.4k104589

























            0














            Every bean managed by Spring container has Singleton scope by default no matter either you use annotation or xml, unless you don't override its default one.






            share|improve this answer
























            • it's nice, but it partly answers the question

              – Andrew Tobilko
              Jan 3 at 9:10
















            0














            Every bean managed by Spring container has Singleton scope by default no matter either you use annotation or xml, unless you don't override its default one.






            share|improve this answer
























            • it's nice, but it partly answers the question

              – Andrew Tobilko
              Jan 3 at 9:10














            0












            0








            0







            Every bean managed by Spring container has Singleton scope by default no matter either you use annotation or xml, unless you don't override its default one.






            share|improve this answer













            Every bean managed by Spring container has Singleton scope by default no matter either you use annotation or xml, unless you don't override its default one.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 8:23









            hiteshhitesh

            12




            12













            • it's nice, but it partly answers the question

              – Andrew Tobilko
              Jan 3 at 9:10



















            • it's nice, but it partly answers the question

              – Andrew Tobilko
              Jan 3 at 9:10

















            it's nice, but it partly answers the question

            – Andrew Tobilko
            Jan 3 at 9:10





            it's nice, but it partly answers the question

            – Andrew Tobilko
            Jan 3 at 9:10


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011005%2fwhat-is-scope-of-spring-class-annoted-using-component-annotation-in-spring-mvc%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter