What Is a Spring Application Context?
Spring contexts are also called Spring IoC containers, which are responsible for instantiating,, and assembling beans by reading configuration Java annotations.
From: https://dzone.com/articles/what-is-a-spring-context
@Service
public class GreetingService {
public void greet() {
System.out.println("Welcomeon Spring!!!");
}
}
@Service,
at the class level, means that this is a service and is eligible to be registered as a bean in the Spring context.
How to register a class in the Spring Context?
By using annotation on the class:
- @Service
- @Component
- @Repository
- @Contoller
- @Bean
Spring Singleton
From: http://javabeat.net/spring-singleton-java-singleton/
The scope of the Spring singleton is best described as per container and per bean. If you define a bean for a particular class in a single container, spring container will create only one instance for the class.
Singleton scope is the default scope for spring beans.
What is a Spring bean?
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
These beans are created with the configuration metadata that you supply to the container, for as annotations @Service, @Controller..etc
Spring Bean Scope
Please note, the only mostly used one is Singleton
- Singleton - (default) Only one instance of the bean is created in the IOC container
Prototype - A new instance is created each time the bean is requested.
Request - A single instance per http request. Only valid in the context of a web-aware Spring ApplicationContext.
Session - A single instance per http session. Only valid in the context of a web-aware Spring ApplicationContext.
Global-session - A single instance per global session. Typically Only used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
Application - bean is scoped to the lifecycle of a ServletContext. Only valid in the context of a web aware.
Websocket - Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
Custom Scope - Spring Scopes are extensible, and you can define your own scope by implementing Spring’s ‘Scope” interface