3

I am thinking about @scope("prototype") scope but I don't get it because we already have the new keyword. Both of them create a new instance of an object in runtime. What is the advantage of using prototype? Is it easier to reallocate than using new? Am I doing something wrong when I create new object with using the new keyword?

I'm trying to understand the advantages of it.

I found this question but it does not explain the advantages / disadvantages of new and prototype: What is the difference between bean with scope prototype and new Object in singleton bean?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Berkin
  • 1,565
  • 5
  • 22
  • 48

4 Answers4

4

By using new keyword you are creating an java object but it is not a spring bean and will not present in spring container. But if you use @scope("prototype") for every request a new spring bean is provided. for more information

What is a spring bean?

A Spring bean is basically an object managed by Spring. More specifically, it is an object that is instantiated, configured and otherwise managed by a Spring Framework container. Spring beans are defined in a Spring configuration file (or, more recently, by using annotations), instantiated by the Spring container, and then injected into your application.

Advantages of spring bean?

1) By default all spring beans are immutable and spring IOC container will manage them

2) By using dependency injection you can inject the beans into any other object easily

3) Dependency injection makes testing easier. The injection can be done through constructor.

4) If you are using spring boot then you can inject the values from application.yml or application.properties into spring beans by using @Value annotation

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • that dependes on injection point. – Antoniossss Sep 24 '19 at 18:58
  • 1
    What is the advantages of initializing inside of spring container? Both of them uses heap memory space. Both of them use garbage collector to reallocate. Thanks by the way – Berkin Sep 24 '19 at 18:59
  • 1
    @Berkin the point is that they can participate in Spring dependency injection. As well as other Spring features like DB transaction boundaries, security checks, etc. You have to let Spring manage the bean for it to participate in all the features Spring provides. – Mark B Sep 24 '19 at 19:07
  • updated my answer @Berkin – Ryuzaki L Sep 24 '19 at 19:10
  • @Deadpool what if you have statefull bean that contains eg current state and cannot be shared between different processes? You need separate instance every time. Thus prototype. – Antoniossss Sep 24 '19 at 19:11
  • I agree with you @Antoniossss but OP is more over looking for advantages between new keyword object and spring beans – Ryuzaki L Sep 24 '19 at 19:16
  • Sorry, mention was to berkin..... your answer is just fine. – Antoniossss Sep 24 '19 at 19:19
3

Its straightforward. In some scenarios you might need separate instance in every bean. It is almost equal to creating with new when needed with an exception that you have the ability to inject dependencies to whatever you have marked as prototype.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
2

For instance you have a token and you should one time consume so every time new instance create to application context for this reason you can use this type.

  • Yeah but we can do such things with using new keyword, can't we? Thanks – Berkin Sep 24 '19 at 19:01
  • Yes you can but when you use with new keyword throughout spring IOC can't include your instance,You are welcome –  Sep 24 '19 at 19:03
1

One of the key benefits of using Spring is that it removes the need to instantiate objects directly via new, rather objects are created as Spring components/beans and registered in the Spring Application Context - this leads to to software components that are available to be composed together with each other via injection, and leads to easier unit testing and more flexible software.

The spring prototype scope means a new instance of the spring component will be created (by the spring runtime/container) each time it is needed and registgered in the spring application context. Ultimately Spring is creating an object and it is stored on the heap like any other object, but the abstraction layer that the Spring Container provides in terms of being able to define and "wire" together software components has profound advantages over using naked new to create objects inside other objects.

0xadecimal
  • 696
  • 5
  • 18