Spring beans list out all beans
In the Spring Framework, a bean is an object that is managed by the Spring IoC (Inversion of Control) container. Spring beans are the backbone of a Spring application, as they are the objects that form the foundation of the application’s structure. The container instantiates, configures, and assembles the beans, handling their lifecycle from creation to destruction.
Types of Spring Beans
1. Singleton Bean:
A single instance of the bean is created per Spring IoC container. This is the default scope.
2. Prototype Bean:
A new instance of the bean is created every time it is requested from the container.
3. Request Bean:
A new instance of the bean is created for each HTTP request. This scope is only valid in a web-aware Spring ApplicationContext.
4. Session Bean:
A new instance of the bean is created for each HTTP session. This scope is only valid in a web-aware Spring ApplicationContext.
5. Global Session Bean:
A new instance of the bean is created for each global HTTP session. This scope is also only valid in a web-aware Spring ApplicationContext, typically used in a portlet context.
6. Application Bean:
A single instance of the bean is created for the lifecycle of a ServletContext.
7. WebSocket Bean:
A new instance of the bean is created for each WebSocket connection.
Table of Contents
Java Examples of Spring Beans
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Singleton Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Prototype Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
@Configuration
public class AppConfig {
@Bean
@Scope(WebApplicationContext.SCOPE_REQUEST)
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Request Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
@Configuration
public class AppConfig {
@Bean
@Scope(WebApplicationContext.SCOPE_SESSION)
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Session Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
@Configuration
public class AppConfig {
@Bean
@Scope(WebApplicationContext.SCOPE_GLOBAL_SESSION)
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Global Session Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;
@Configuration
public class AppConfig {
@Bean
@Scope(WebApplicationContext.SCOPE_APPLICATION)
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("Application Bean: Serving...");
}
}
```
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.socket.WebSocketSession;
@Configuration
public class AppConfig {
@Bean
@Scope("websocket")
public MyService myService() {
return new MyService();
}
}
public class MyService {
public void serve() {
System.out.println("WebSocket Bean: Serving...");
}
}
```