Spring Flashcards
What is Spring?
Spring is an application development framework for java. Spring is an open-source framework, it used to create high performance, easily testable and reusable codes.
What Spring modules have you used?
The core container (which consist of the bean)
The ORM module (JPA) for data access
The web-servlet modules which provides Spring’s mvc (model-view-controller) implementation for web-application.
What is a bean?
Beans are objects managed by spring, that live in spring’s application context and can be injected into an existing class.
What is dependency injection?
Is used to connect a class with its dependencies. It is also loose coupled so dependencies can be injected during the runtime. Dependencies are defined by the bean configuration. The methods to inject dependencies are Constructor Injection and Setter Injection.
What is the Spring IOC container?
IOC –Inversion of Control is the transfer of control of objects or portions of a program to a container or framework. Spring has been given the power to decide on how to inject or instantiate objects.
Could you tell me about the BeanFactory and ApplicationContext, and the difference between the two?
BeanFactory is an interface for creating and managing beans. It is the core container in spring and provides the basic functionality for instantiating and managing beans.
While ApplicationContext is the subset of BeanFactory and provides event propagation, AOP and more. Their difference is that BeanFactory provides basic functionality and ApplicationContext provides advanced functionality in an application.
“ApplicationContext extends BeanFactory and loads the beans eagerly, on startup”
What does the @Bean annotation do?
It is applied on a method to specify that it returns a bean to be instantiated, assembled, and managed by the Spring IoC container. (Method-level annotation)
What does the @Component annotation do?
@Component is the main stereotype annotation. It is a class-level annotation. It is used across the application to mark the bean as Spring’s managed components.
What does the @Autowired annotation do?
Is used for automatic dependency injection. ( autowiring happens by placing an instance of one bean into the desire field in an instance of another bean)
What are the different ways to perform dependency injection using autowiring (places to put the autowired annotation)?
Field Injection, Constructor Injection, Setter Injection.
What are the different scopes of a bean, and what is the default scope?
The Scopes of a bean are Singleton and prototype.
- For Singleton, the container creates an instance of that bean. It is defined by using @scope (“singleton”) annotation. All requests for that bean name will return the same object. Singleton creates a single instance of the class for an Application Context. The scope is the default value if no other scope is specified.
- For Prototype, this scope will return a different instance every time it is requested from the container. It is defined by @scope(“prototype”) annotation. prototype bean is not initialized by Spring IoC until an object is created
Could you tell me about the steps of the lifecycle of a bean?
-Instantiation-which creates an instance of the bean
-Dependency injection- injects all dependencies of the bean
-Initialization- initialization of methods after all dependencies have been injected.
-Use- when the bean is used
-Destruction- when spring container calls the bean’s destruction method before destroying the bean
-Disposal-when the bean instance removes from the application.
What are stereotype annotations?
These annotations are used to create Spring beans automatically in ApplicationContext
Included are: @Component, @Service, @Repository, and @Controller
Because they are stereotypes, they can inform Spring how to configure a certain bean, or, they can just be semantic help for a developer reading a class
What does the application.properties file do?
Properties files are used to keep ‘N’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. The application.properties file is located in the src/main/resources directory.
Why would a developer use Spring (core)?
Spring makes programming Java quicker, easier, and safer. It enables you to build applications from “plain old Java objects” and to apply enterprise services non-evasively to POJOs. Spring helps us focus on business logic and core tasks and Spring will manage the infrastructure. (advantage of loose coupling)
What is MVC?
MVC is a java framework used to build web applications. It follows the Model-View- Controller design pattern. It allows developers to separate the concerns of an application into three distinct layers: the model (which represents the business logic and data), the view (which presents the data to the user), and the controller (which handles user input and manages the flow of the application).