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).
What does the RequestMapping annotation do?
It is used to map web requests onto specifics handler classes or handler methods.
@RequestMapping(“/Home”).
What does the Response body annotation do?
The @ResposeBody annotation tells a controller that the objects returned is automatically serialized into JSON and passed back into the HTTPRespose object.
How is the RestController annotation different from the Controller annotation?
@Controller can return HTTP Responses while @RestController can return JSON responses. Both are used to define web controllers as per MVC design.
What other annotations are used in Spring MVC?
@RequestBody – maps the body of an HTTP Request to an object
@PathVariable – indicates a method argument is bound to the URI template variable, can be set to be an optional variable
@RequestParam – indicates a method argument is bound to the URI template variable, can be set to have a default value
@ExceptionHandler – declares a custom error handler method based on the passed error class
@ResponseStatus – specifies the desired HTTP status of the response, usually used with @ExceptionHandler
@ModelAttribute – allows access to elements that are already in the model
@CrossOrigin – enables cross-domain communication for annotated request handler methods
What is an Object Relational Mapping?
A way of accessing data from a database server. An ORM framework constructs the code necessary to fill in SQL statements based on the construction of the model itself.
ORM covers many technologies such as JPA, JDO, Hibernate, and Oracle Toplin/iBATIS
What is an ORM entity?
A class that is correlated with a table in the database. Each object instantiated by this class indicates a tuple of the table itself.
Why would a developer use an ORM?
- Less coding is required: don’t need to write code before and after actual database logic such as: getting the connection, starting transaction, commiting transaction, closing connection, ect.
- Easy to Test: Spring IOC makes it easy to test the applicaion
- Better exception handling: Spring provides its own API for exception handling with ORM
- Integrated transaction management: We can wrap our mapping code with an explicit template wrapper class or AOP style method intereptor
What annotations are used to create your ORM entity?
@Entity to create an entity
@Table, @Column, and @Id are often used to define how a table is constructed.
@OneToMany, @ManyToOne, @JoinColum, and @JsonBackReference.