What is the purpose of scope = "prototype" in spring?

From what I understand, if scope = "prototype" is set for a bean, a new instance of this class is created for each request. So why would someone use spring to create a new object instead of just writing

ClassName newBean = new ClassName();

I used spring to create single numbers.

+3
source share
3 answers

Because you can add other beans to your prototype. Or you can apply AOP aspects to the methods of this bean. Or you can make it transactional. Or safe.

If you simply call the class constructor, it is no longer a Spring-installed bean, and all Spring with beans does not execute with a manually created object.

+5
source

, spring ObjectFactory struts2. Action HTTP-.

+2

CMOE = "Close for modification, Open for extension."

If there is any behavior change for ClassName, you can extend ClassName and provide the object of the new class in config.

This will not require code changes.

This is one of the simple reasons. What JB Nizet said is more descriptive and in-depth and.

0
source

All Articles