Spring Flashcards
spring framework
opensource java application framework which uses two principles, dependecy injection and inversion of control
Spring is not one big framework. It is broken down into modules. This can be seen in the Maven Dependencies folder, where there are a lot of JAR files instead of just one big JAR.
This enables some modules to be used without using the whole framework
inversion of control
Traditionally, the class which needed the dependency created an instance of the dependency. The class decided when to create the dependency and how to create it.
Spring takes this responsibility from the class and creates the object itself. The developer simply mentions the dependency and the framework takes care of the rest.
dependency injection
It is the process by which Spring looks up the beans that are needed for a particular bean to function and injects them as a dependency.
Spring can perform dependency injection by using constructor or by using a setter method.
IOC Container
is a framework that provides the Inversion of Control functionality.
Spring offers two implementations of IOC container
Bean factory - legacy IoC container and provides basic management for beans and wiring of dependencies.
Application context - It includes basic management of beans and wiring of dependencies as provided by the bean factory. Additional features in application context include Spring AOP features, internationalization, web application context, etc.
Modules of spring architecture
Autowiring approaches
in order:
qualifier - if i specify the name in qualifier, then that bean will be selected
primary - if primary is present, then if no qualifier is given, it will take the primary bean
name
@Primary annotation should be used if there is one clear favorite to be used in a majority of situations. In some cases, one algorithm might be more efficient or more important than the rest and is declared as the primary choice. The bean with @Primary gets chosen unless another bean is required, which can be specified with @Qualifier. The bean with @Qualifier is only used to request an alternate bean in case the primary choice is not required.
@Autowired annotation resolves dependencies by type. If there are more than one beans of the same type, a conflict arises. We have seen three different approaches to resolve conflicts. They can be resolved using the @Primary annotation, renaming the variable to match the name of the class, or by using the @Qualifier annotation.
Bean scopes
There are six types of scopes: singleton, prototype, request, session, application, and websocket.
Relevant Scopes for REST API Development
Singleton Scope
Prototype Scope
Request Scope
Session Scope
. Singleton Scope
@Service
Description: A single instance of the bean is created and shared across the entire Spring container. This is the default scope in Spring.
Use Case: Typically used for stateless services, configuration classes, and beans that are shared across the application.
Relevance: Highly relevant for REST API development as many services and repositories are designed to be stateless and shared.
- Prototype Scope
@Scope(“prototype”)
@Component
Description: A new instance of the bean is created each time it is requested from the Spring container.
Use Case: Used for stateful beans or beans that hold transient data.
Relevance: Less common in REST API development but useful when you need a new instance for each request or operation - Request Scope
@Scope(“request”)
@Component
Description: A new instance of the bean is created for each HTTP request. It is destroyed once the request is completed.
Use Case: Used for beans that need to be aware of and tied to the lifecycle of an HTTP request.
Relevance: Very relevant for REST API development, especially for components like request-scoped services or filters that need to process data specific to a single HTTP request. - Session Scope
@Scope(“session”)
@Component
Description: A new instance of the bean is created for each HTTP session. It is destroyed when the session is invalidated.
Use Case: Used for beans that need to maintain state across multiple requests within the same HTTP session.
Relevance: Useful for REST APIs that need to manage user sessions, such as authentication and user session data.
Spring vs. Gang of Four singleton#
in Spring it means one bean per application context. By the GoF definition, even if there were more than one application contexts running on the same JVM, there would still be only one instance of the singleton class.
SINGLETON VS PROTYPE BEAN SCOPE
Spring creates a singleton bean even before we ask for it while a prototype bean is not created till we request Spring for the bean
what happens if i inject prototype bean into singleton bean ( means dependent of singleton is a prototype)
The prototype bean is injected into the singleton bean at the time of creation of the singleton bean when the container initializes it.
When a prototype bean is injected into a singleton bean, it loses its prototype behavior and acts as a singleton.
we can use proxymode to overcome this behaviour
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode=ScopedProxyMode.TARGET_CLASS)
what is the use of @ComponentScan
@ComponentScan(basePackages={“io.datajek.spring.basics.movierecommendersystem.lesson9”,
“io.datajek.spring.basics.movierecommendersystem.lesson10”})
Spring will look for beans in the packages specified
we can also specify filter to further refine the scan
FilterType.ANNOTATION
FilterType.ASPECTJ
FilterType.ASSIGNABLE_TYPE
FilterType.REGEX
FilterType.CUSTOM
eg:@ComponentScan(basePackages = “io.datajek.spring.basics.movierecommendersystem.lesson10”)
@ComponentScan(includeFilters = @ComponentScan.Filter (
type= FilterType.REGEX,
pattern=”io.datajek.spring.basics.movierecommendersystem.lesson9.*”))
check if a package contains bean
appContext.containsBean(“collaborativeFilter”)
@preDestroy
The method having this annotation is called when the bean is in the process of being removed from the container. All cleanup stuff can be performed in this method. A method with the @PreDestroy annotation can be used to release resources or close a database connection.
@postConstruct
If the developer wants to perform a task after the dependencies have been populated, it can be done by calling a method annotated with the @PostConstruct annotation.
The method can have any name and its return type is always void. After the bean is created, we can initialize the contents of the bean, load data, establish a database connection, or connect to a web server. The post construct method is only called after all the dependencies have been populated.
CDI Annotations
It is the Java EE standard. if the application is migrated to another framework, the cdi annotatons are valid intead of the spring specific annotations.
@Named can be used instead of @Component
@Inject instead of @Autowired