Spring Flashcards

1
Q

What is Spring?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What Spring modules have you used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a bean?

A

Beans are objects managed by spring, that live in spring’s application context and can be injected into an existing class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is dependency injection?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Spring IOC container?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Could you tell me about the BeanFactory and ApplicationContext, and the difference between the two?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the @Bean annotation do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the @Component annotation do?

A

@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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the @Autowired annotation do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the different ways to perform dependency injection using autowiring (places to put the autowired annotation)?

A

Field Injection, Constructor Injection, Setter Injection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the different scopes of a bean, and what is the default scope?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Could you tell me about the steps of the lifecycle of a bean?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are stereotype annotations?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the application.properties file do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why would a developer use Spring (core)?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is MVC?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the RequestMapping annotation do?

A

It is used to map web requests onto specifics handler classes or handler methods.

@RequestMapping(“/Home”).

18
Q

What does the Response body annotation do?

A

The @ResposeBody annotation tells a controller that the objects returned is automatically serialized into JSON and passed back into the HTTPRespose object.

19
Q

How is the RestController annotation different from the Controller annotation?

A

@Controller can return HTTP Responses while @RestController can return JSON responses. Both are used to define web controllers as per MVC design.

20
Q

What other annotations are used in Spring MVC?

A

@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

21
Q

What is an Object Relational Mapping?

A

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

22
Q

What is an ORM entity?

A

A class that is correlated with a table in the database. Each object instantiated by this class indicates a tuple of the table itself.

23
Q

Why would a developer use an ORM?

A
  • 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
24
Q

What annotations are used to create your ORM entity?

A

@Entity to create an entity
@Table, @Column, and @Id are often used to define how a table is constructed.
@OneToMany, @ManyToOne, @JoinColum, and @JsonBackReference.

25
Q

How do you create the Spring Data JPARepository, and what does it do?

A

Define an interface that extends the JpaRepository interface.

The JpaRepository interface is a generic interface that has 2 type parameters: The type of the Entity and the type of its Primary Key.

26
Q

How do you create a custom query in a JPARepository?

A

inside your repository, you will use the @Query annotation.

the value will be the SQL or JPQL statement to execute

Example: @Query (“SELECT u FROM u WHERE u.status = 1”)

27
Q

What is JPQL?

A

JPQL is Java Persistence Query Language defined in JPA specification. It is used to create queries against entities to store in a relational database. JPQL is developed based on SQL syntax.

28
Q

Why would a developer use JPQL instead of native SQL?

A

Portability – JPQL is database independent

If we want to change dialects of SQL, then JPQL can be converted into any dialect. If we were using native SQL, we’d have to convert all the existing queries to match the syntax of the new dialect

Object-Oriented approach – JPQL is written using entities and relationships

Type Safety – JPQL is strongly typed: the compiler checks for type errors

Query Abstraction – the underlying database is abstracted away which makes it easier to modify without changing queries

Security – provides some level of SQL injection prevention

29
Q

What does AutoDDL do?

A

A feature in spring that updates or creates database scheme at runtime based on the defined Jpa Entities and their relationships.

It saves developer time by automatically updating the database structure if changes are made in the code.

30
Q

What does the @Transactional annotation do?

A

Transactional is to configure the transactional behavior of a method. It is required to start the transaction, commit, or roll back a transaction.

It also integrates with Hibernate and JPA.

Applied using AOP

31
Q

What is a cross-cutting concern?

A

A cross-cutting concern is a concern that is applicable throughout the application (or more than one module). e.g. logging, security and data transfer are the concerns needed in almost every module of an application, hence they are termed as cross-cutting concerns.

32
Q

Could you explain what an Aspect is?

A

This is a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.

33
Q

Why would a developer use AOP?

A

AOP provides a way to dynamically add the cross-cutting concern before, after or around the actual logic using simple pluggable configurations. It also makes it easy to maintain code in the present and future. You can add/remove concerns without recompiling complete source code simply by changing configuration files

34
Q

What annotations does The @SpringBootApplication include?

A

@EnableAutoConfiguration: It automatically creates and registers beans based on the jar files in the classpath and configures it to run methods.

@ComponentScan: tells Spring to scan for components. It looks for @controller, @service, and other components we define. It is used with the @Configuration annotation to specify the package where Spring needs to scan for components.

@Configuration: Most important annotation! It’s part of the Spring core framework and indicates the class has @Bean definition methods. So that the spring container can process the class and generate Spring beans.

35
Q

Why would a developer use Spring Boot instead of Spring without Boot?

A

Faster development time: reduces amount of boiletplate, allowing developers to focus on logic rather than configuration.

Auto-configuration: Many common configurations are handled automatically.

Allows building standalone applications: It simplifies the process.

Easier to launch, widely use for building REST Api.

36
Q

Why would a developer use Spring Actuator?

A

We use Actuator to expose operational information about the running applications such as health, metrics, info, dump, env, and more. We would get this information through actuator endpoints.

37
Q

Why would a developer use Spring Devtools?

A

A developer would use Spring devtools to improve development time while working on Spring Boot Applications. Some feature added are property defaults, automatic restart, liveReload embedded server, remote debugging, remote update

38
Q

Why would a developer use Spring Environments?

A

To externalize configurations to work on the same code in different environments.

39
Q

What is Lombok?

A

Project Lombok) is an annotation-based Java library that allows you to reduce boilerplate code. Lombok offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write.

40
Q

Why would a developer use Lombok?

A

Lombok speeds up development by generating common boilerplate code for a developer.

It allows developers to focus on business logic instead of the tedious syntax required to code constructors, getter, setters, hashCode, and other basic class methods.