Spring beans list out all the beans
Spring Beans are objects that are managed by the Spring IoC (Inversion of Control) container. These beans are created, initialized, and managed by the Spring container, and they are used to build the application components. Beans can be of various types and can be configured in different ways.
Types of Spring Beans:
- Singleton Beans: The default scope where a single instance of the bean is created and shared across the application.
- Prototype Beans: A new instance of the bean is created every time it is requested from the container.
- Request-scoped Beans: A new instance is created for each HTTP request in a web application.
- Session-scoped Beans: A new instance is created for each HTTP session in a web application.
- Global Session-scoped Beans: A new instance is created for each global HTTP session in a web application.
Table of Contents
Examples
1. Singleton Bean
Definition: A singleton bean is the default bean scope. Only one instance of the bean is created and shared across the application.
```java
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class SingletonBean {
public void showMessage() {
System.out.println("This is a singleton bean.");
}
}
```
Usage:
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
private final SingletonBean singletonBean;
@Autowired
public DemoComponent(SingletonBean singletonBean) {
this.singletonBean = singletonBean;
}
public void printMessage() {
singletonBean.showMessage();
}
}
```
Explanation
@Component
indicates thatSingletonBean
is a Spring-managed bean with singleton scope by default. TheDemoComponent
class injects the singleton bean and uses it.
2. Prototype Bean
Definition: A prototype bean is created every time it is requested from the container.
Example:
```java
package com.example.demo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class PrototypeBean {
public void showMessage() {
System.out.println("This is a prototype bean.");
}
}
```
Usage:
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
@Autowired
private ApplicationContext context;
public void printMessage() {
PrototypeBean prototypeBean1 = context.getBean(PrototypeBean.class);
PrototypeBean prototypeBean2 = context.getBean(PrototypeBean.class);
prototypeBean1.showMessage();
prototypeBean2.showMessage();
System.out.println(prototypeBean1 == prototypeBean2); // false, different instances
}
}
```
Explanation: @Scope("prototype")
specifies that PrototypeBean
will be instantiated every time it is requested. DemoComponent
shows that different instances are created.
3. Request-scoped Bean
Definition: A request-scoped bean is created for each HTTP request.
Example:
```java
package com.example.demo;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.stereotype.Component;
@Component
@RequestScope
public class RequestScopedBean {
public void showMessage() {
System.out.println("This is a request-scoped bean.");
}
}
```
Usage:
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
@Autowired
private RequestScopedBean requestScopedBean;
public void printMessage() {
requestScopedBean.showMessage();
}
}
```
Explanation: @RequestScope
specifies that RequestScopedBean
will be created for each HTTP request. This is often used in web applications.
4. Session-scoped Bean
Definition: A session-scoped bean is created for each HTTP session.
Example:
```java
package com.example.demo;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.stereotype.Component;
@Component
@SessionScope
public class SessionScopedBean {
public void showMessage() {
System.out.println("This is a session-scoped bean.");
}
}
```
Usage:
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
@Autowired
private SessionScopedBean sessionScopedBean;
public void printMessage() {
sessionScopedBean.showMessage();
}
}
```
Explanation: @SessionScope
specifies that SessionScopedBean
will be created for each HTTP session. This scope is useful for session-related data.
5. Global Session-scoped Bean
Definition: A global session-scoped bean is created for each global HTTP session (not commonly used).
Example:
```java
package com.example.demo;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.stereotype.Component;
@Component
@Scope("globalSession")
public class GlobalSessionScopedBean {
public void showMessage() {
System.out.println("This is a global session-scoped bean.");
}
}
```
Usage:
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
@Autowired
private GlobalSessionScopedBean globalSessionScopedBean;
public void printMessage() {
globalSessionScopedBean.showMessage();
}
}
```
Explanation: @Scope("globalSession")
specifies that GlobalSessionScopedBean
is created for each global HTTP session.
Conclusion
- Spring Beans: Managed by the Spring IoC container and can be of various types, including singleton, prototype, request-scoped, session-scoped, and global session-scoped.
- Examples: Demonstrated different bean scopes with annotations and their usage in a Spring Boot application.