What is the best practice: Use prototype bean instead of new () operator












4















I am trying to understand what would be the correct usage of Spring prototype bean.
May be the following code sample will help in you understanding my dilemma:



List<ClassA> caList = new ArrayList<ClassA>();
for (String name : nameList) {
ClassA ca = new ClassA();

//or Shall I use protypebean, using method lookup I can inject the dependency of ClassA.
// ClassA ca = getPrototypeClassA();

ca.setName(name);
caList.add(ca);
}


So my exact point is in this scenario shall I go with method injection or new() operator.
Provide your view with justification.










share|improve this question























  • Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

    – Erwin Bolwidt
    Jan 3 at 6:02
















4















I am trying to understand what would be the correct usage of Spring prototype bean.
May be the following code sample will help in you understanding my dilemma:



List<ClassA> caList = new ArrayList<ClassA>();
for (String name : nameList) {
ClassA ca = new ClassA();

//or Shall I use protypebean, using method lookup I can inject the dependency of ClassA.
// ClassA ca = getPrototypeClassA();

ca.setName(name);
caList.add(ca);
}


So my exact point is in this scenario shall I go with method injection or new() operator.
Provide your view with justification.










share|improve this question























  • Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

    – Erwin Bolwidt
    Jan 3 at 6:02














4












4








4








I am trying to understand what would be the correct usage of Spring prototype bean.
May be the following code sample will help in you understanding my dilemma:



List<ClassA> caList = new ArrayList<ClassA>();
for (String name : nameList) {
ClassA ca = new ClassA();

//or Shall I use protypebean, using method lookup I can inject the dependency of ClassA.
// ClassA ca = getPrototypeClassA();

ca.setName(name);
caList.add(ca);
}


So my exact point is in this scenario shall I go with method injection or new() operator.
Provide your view with justification.










share|improve this question














I am trying to understand what would be the correct usage of Spring prototype bean.
May be the following code sample will help in you understanding my dilemma:



List<ClassA> caList = new ArrayList<ClassA>();
for (String name : nameList) {
ClassA ca = new ClassA();

//or Shall I use protypebean, using method lookup I can inject the dependency of ClassA.
// ClassA ca = getPrototypeClassA();

ca.setName(name);
caList.add(ca);
}


So my exact point is in this scenario shall I go with method injection or new() operator.
Provide your view with justification.







java spring prototype javabeans






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 5:45









008ak89008ak89

6526




6526













  • Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

    – Erwin Bolwidt
    Jan 3 at 6:02



















  • Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

    – Erwin Bolwidt
    Jan 3 at 6:02

















Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

– Erwin Bolwidt
Jan 3 at 6:02





Do you have anything to inject? Can't you separate out the things you need to inject from the mutable state if the object (if you can, be next the stateless bean) - then you could use a prototype bean.

– Erwin Bolwidt
Jan 3 at 6:02












3 Answers
3






active

oldest

votes


















3














You can make use of either of the ways, because ultimately client code is responsible for handling the life-cycle of the prototype bean rather than spring container.



According to Spring-docs,




In some respects, you can think of the Spring containers role when
talking about a prototype-scoped bean as somewhat of a replacement for
the Java 'new' operator.
All lifecycle aspects past that point have to
be handled by the client.



Spring does not manage the complete lifecycle of a prototype bean: the
container instantiates, configures, decorates and otherwise assembles
a prototype object, hands it to the client and then has no further
knowledge of that prototype instance. It is the responsibility of the
client code to clean up prototype scoped objects and release any
expensive resources that the prototype bean(s) are holding onto.







share|improve this answer
























  • No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

    – 008ak89
    Jan 7 at 12:38



















0














It seems that your instance need some runtime values in order to initialise properly. In such case ,it depends on if you need to use spring feature such as AOP for the ClassA instance. If yes , go with the method injection .If not , you can consider using factory pattern . Much more OO and cleaner to me :



Something like the following . You should get the idea.



@Component
public class FactoryForClassA {

@Autowired
private FooBean someDependencyForClassA;


public ClassA create(String name){
ClassA a = new ClassA(someDependencyForClassA);
a.setName(name);
return a;
}
}


And the client code:



@Autowired
private FactoryForClassA factoryForClassA;

List<ClassA> caList = new ArrayList<ClassA>();
for (String name : nameList) {
ClassA a = factoryForClassA.create(name);
caList.add(ca);
}





share|improve this answer































    0














    If ClassA needs to have @Autowired references, then go for a prototype bean.



    Otherwise a simple POJO (that the Spring container is unaware of) will do.






    share|improve this answer























      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%2f54016936%2fwhat-is-the-best-practice-use-prototype-bean-instead-of-new-operator%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You can make use of either of the ways, because ultimately client code is responsible for handling the life-cycle of the prototype bean rather than spring container.



      According to Spring-docs,




      In some respects, you can think of the Spring containers role when
      talking about a prototype-scoped bean as somewhat of a replacement for
      the Java 'new' operator.
      All lifecycle aspects past that point have to
      be handled by the client.



      Spring does not manage the complete lifecycle of a prototype bean: the
      container instantiates, configures, decorates and otherwise assembles
      a prototype object, hands it to the client and then has no further
      knowledge of that prototype instance. It is the responsibility of the
      client code to clean up prototype scoped objects and release any
      expensive resources that the prototype bean(s) are holding onto.







      share|improve this answer
























      • No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

        – 008ak89
        Jan 7 at 12:38
















      3














      You can make use of either of the ways, because ultimately client code is responsible for handling the life-cycle of the prototype bean rather than spring container.



      According to Spring-docs,




      In some respects, you can think of the Spring containers role when
      talking about a prototype-scoped bean as somewhat of a replacement for
      the Java 'new' operator.
      All lifecycle aspects past that point have to
      be handled by the client.



      Spring does not manage the complete lifecycle of a prototype bean: the
      container instantiates, configures, decorates and otherwise assembles
      a prototype object, hands it to the client and then has no further
      knowledge of that prototype instance. It is the responsibility of the
      client code to clean up prototype scoped objects and release any
      expensive resources that the prototype bean(s) are holding onto.







      share|improve this answer
























      • No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

        – 008ak89
        Jan 7 at 12:38














      3












      3








      3







      You can make use of either of the ways, because ultimately client code is responsible for handling the life-cycle of the prototype bean rather than spring container.



      According to Spring-docs,




      In some respects, you can think of the Spring containers role when
      talking about a prototype-scoped bean as somewhat of a replacement for
      the Java 'new' operator.
      All lifecycle aspects past that point have to
      be handled by the client.



      Spring does not manage the complete lifecycle of a prototype bean: the
      container instantiates, configures, decorates and otherwise assembles
      a prototype object, hands it to the client and then has no further
      knowledge of that prototype instance. It is the responsibility of the
      client code to clean up prototype scoped objects and release any
      expensive resources that the prototype bean(s) are holding onto.







      share|improve this answer













      You can make use of either of the ways, because ultimately client code is responsible for handling the life-cycle of the prototype bean rather than spring container.



      According to Spring-docs,




      In some respects, you can think of the Spring containers role when
      talking about a prototype-scoped bean as somewhat of a replacement for
      the Java 'new' operator.
      All lifecycle aspects past that point have to
      be handled by the client.



      Spring does not manage the complete lifecycle of a prototype bean: the
      container instantiates, configures, decorates and otherwise assembles
      a prototype object, hands it to the client and then has no further
      knowledge of that prototype instance. It is the responsibility of the
      client code to clean up prototype scoped objects and release any
      expensive resources that the prototype bean(s) are holding onto.








      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 3 at 6:14









      AnkurAnkur

      32419




      32419













      • No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

        – 008ak89
        Jan 7 at 12:38



















      • No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

        – 008ak89
        Jan 7 at 12:38

















      No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

      – 008ak89
      Jan 7 at 12:38





      No that is fine, so my point in spring project we can bypass prototype scoped beans in every scenario...So there is no real use of prototype bean in spring.

      – 008ak89
      Jan 7 at 12:38













      0














      It seems that your instance need some runtime values in order to initialise properly. In such case ,it depends on if you need to use spring feature such as AOP for the ClassA instance. If yes , go with the method injection .If not , you can consider using factory pattern . Much more OO and cleaner to me :



      Something like the following . You should get the idea.



      @Component
      public class FactoryForClassA {

      @Autowired
      private FooBean someDependencyForClassA;


      public ClassA create(String name){
      ClassA a = new ClassA(someDependencyForClassA);
      a.setName(name);
      return a;
      }
      }


      And the client code:



      @Autowired
      private FactoryForClassA factoryForClassA;

      List<ClassA> caList = new ArrayList<ClassA>();
      for (String name : nameList) {
      ClassA a = factoryForClassA.create(name);
      caList.add(ca);
      }





      share|improve this answer




























        0














        It seems that your instance need some runtime values in order to initialise properly. In such case ,it depends on if you need to use spring feature such as AOP for the ClassA instance. If yes , go with the method injection .If not , you can consider using factory pattern . Much more OO and cleaner to me :



        Something like the following . You should get the idea.



        @Component
        public class FactoryForClassA {

        @Autowired
        private FooBean someDependencyForClassA;


        public ClassA create(String name){
        ClassA a = new ClassA(someDependencyForClassA);
        a.setName(name);
        return a;
        }
        }


        And the client code:



        @Autowired
        private FactoryForClassA factoryForClassA;

        List<ClassA> caList = new ArrayList<ClassA>();
        for (String name : nameList) {
        ClassA a = factoryForClassA.create(name);
        caList.add(ca);
        }





        share|improve this answer


























          0












          0








          0







          It seems that your instance need some runtime values in order to initialise properly. In such case ,it depends on if you need to use spring feature such as AOP for the ClassA instance. If yes , go with the method injection .If not , you can consider using factory pattern . Much more OO and cleaner to me :



          Something like the following . You should get the idea.



          @Component
          public class FactoryForClassA {

          @Autowired
          private FooBean someDependencyForClassA;


          public ClassA create(String name){
          ClassA a = new ClassA(someDependencyForClassA);
          a.setName(name);
          return a;
          }
          }


          And the client code:



          @Autowired
          private FactoryForClassA factoryForClassA;

          List<ClassA> caList = new ArrayList<ClassA>();
          for (String name : nameList) {
          ClassA a = factoryForClassA.create(name);
          caList.add(ca);
          }





          share|improve this answer













          It seems that your instance need some runtime values in order to initialise properly. In such case ,it depends on if you need to use spring feature such as AOP for the ClassA instance. If yes , go with the method injection .If not , you can consider using factory pattern . Much more OO and cleaner to me :



          Something like the following . You should get the idea.



          @Component
          public class FactoryForClassA {

          @Autowired
          private FooBean someDependencyForClassA;


          public ClassA create(String name){
          ClassA a = new ClassA(someDependencyForClassA);
          a.setName(name);
          return a;
          }
          }


          And the client code:



          @Autowired
          private FactoryForClassA factoryForClassA;

          List<ClassA> caList = new ArrayList<ClassA>();
          for (String name : nameList) {
          ClassA a = factoryForClassA.create(name);
          caList.add(ca);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 6:49









          Ken ChanKen Chan

          41.3k1596114




          41.3k1596114























              0














              If ClassA needs to have @Autowired references, then go for a prototype bean.



              Otherwise a simple POJO (that the Spring container is unaware of) will do.






              share|improve this answer




























                0














                If ClassA needs to have @Autowired references, then go for a prototype bean.



                Otherwise a simple POJO (that the Spring container is unaware of) will do.






                share|improve this answer


























                  0












                  0








                  0







                  If ClassA needs to have @Autowired references, then go for a prototype bean.



                  Otherwise a simple POJO (that the Spring container is unaware of) will do.






                  share|improve this answer













                  If ClassA needs to have @Autowired references, then go for a prototype bean.



                  Otherwise a simple POJO (that the Spring container is unaware of) will do.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 19:09









                  Markus PscheidtMarkus Pscheidt

                  3,53233053




                  3,53233053






























                      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%2f54016936%2fwhat-is-the-best-practice-use-prototype-bean-instead-of-new-operator%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas