When to use Spring prototype spacebar?

I want to know when should I use scope prototypein Spring? I realized that it singletonreturns the same instance of the object if a bean is requested.

Then why should we consider prototype?

Explanations with examples will help many understand the need for it.

+17
source share
3 answers

To be clear, simple definitions:

  • Prototype Area = A new object is created each time it is entered / viewed. He will use a new SomeBean()one every time.

  • Singleton scope = , /. SomeBean, .

bean . , statefull beans, beans. bean .

:

(DAO) , DAO ; ​​ .

+14

. prototype, / , .

, , 2 000 000 , 5 , : , .

, , .

, , , Truck, , - VehicleGrupConfiguration, , : , , ... ..

, , , .

+6

, bean- Foo :

Foo foo = new Foo(dependency1, dependency2, ...);
foo.initialize(dependency7, dependency8...);

bean- new, , , , .

, , EJB2 Java Entity Bean, ,

Person p = ...
p.setName("John Doe");
p.save(); // write to DB

JPA

Person p = new Person();
p.setName("John Doe");
personService.save(p); // write to DB

EJB , , , , .

: - Java- SimpleDateFormat , (, ). , , ( spring), , , .

0

All Articles