# Spring Bean

<https://medium.com/omarelgabrys-blog/spring-a-head-start-beans-configuration-part-2-4a8c239b070a>

## 1. Spring Bean Scopes and Life Cycle - XML

**Scopes**

defines the scope of the bean object (singleton, prototype, etc). Spring supports five scopes, three of them are available only with the web apps.The default scope is “singleton”, while the “prototype” scope will create a new object every time we instantiate an object.

```markup
<bean id = "app" class = "App" scope="prototype"></bean>
```

[With prototype scope, beans already are lazy instantiated](https://stackoverflow.com/questions/2365677/creating-lazily-initialized-spring-beans-using-annotation-based-configuration). So, lazy instantiation is only useful when the scope is singleton.

**Lifecycle** of the bean is as the following:

*Bean Instantiated -> Dependencies Injected -> Internal Processing -> Custom Init and destroy method(hooks) -> Bean is ready for use*.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lin-4.gitbook.io/java-ee-notebook/spring-bean.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
