Spring Flashcards

1
Q

What do you mean by IoC (Inversion of Control) Container?

A

Inversion of Control is a principle in software engineering which transfers the control of objects or portions of a program to a container or framework. We most often use it in the context of object-oriented programming.

In contrast with traditional programming, in which our custom code makes calls to a library, IoC enables a framework to take control of the flow of a program and make calls to our custom code. To enable this, frameworks use abstractions with additional behavior built in. If we want to add our own behavior, we need to extend the classes of the framework or plugin our own classes.

The advantages of this architecture are:

a) decoupling the execution of a task from its implementation
making it easier to switch between different implementations
b) greater modularity of a program
c) greater ease in testing a program by isolating a component or mocking its dependencies, and allowing components to communicate through contracts
d) We can achieve Inversion of Control through various mechanisms such as: Strategy design pattern, Service Locator pattern, Factory pattern, and Dependency Injection (DI).

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

What are two ways of injecting beans in java? What are they pros and cons?

A

In Java, the 2 major ways of achieving dependency injection are:

a) Constructor injection: Here, the IoC container invokes the class constructor with a number of arguments where each argument represents a dependency on the other class.

b) Setter injection: Here, the spring container calls the setter methods on the beans after invoking a no-argument static factory method or default constructor to instantiate the bean

c) Field injection
Field is annotated with @Autowired

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

How is the configuration meta data provided to the spring container?

A

a) XML-Based configuration
b) Annotation-Based configuration
c) Java-based configuration

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

What are the bean scopes available in Spring?

A

The Spring Framework has five scope supports. They are:

a) Singleton: The scope of bean definition while using this would be a single instance per IoC container.
b) Prototype: Here, the scope for a single bean definition can be any number of object instances.
c) Request: The scope of the bean definition is an HTTP request.
d) Session: Here, the scope of the bean definition is HTTP-session.

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

What is Dependency Injection?

A

Dependency Injection is a software design technique in which the creation and binding of dependencies are done outside of the dependent class. Afterwards, said dependencies are provided already instantiated and ready to be used, hence the term “injection”; in contrast to the dependent class having to instantiate its dependencies internally, and having to know how to configure them, thus causing coupling.

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

What is the difference between the Dependency Inversion Principle (DIP) and Dependency Injection?

A

DIP:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.

Eg. the Service class with business logic should not directly use the Repository class. Instead in the module with business logic, we should provide an interface used by the service class and the Repository class should implement this behavior.

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

What is Spring AOP?

https://nullpointerexception.pl/jak-korzystac-ze-spring-aop/

A

AOP - Aspect Oriented Programming

Programowanie zorientowane aspektowo (z ang. aspect-oriented programming – w springu jest to moduł Spring AOP) to sposób programowania, który pozwala oddzielić pewne fragmenty kodu od siebie, poprawiając dzięki temu modularność tworzonego programu. Można na to spojrzeć tak, że programowanie aspektowe pozwala wprowadzić dodatkową warstwę do kodu, który tworzymy i zwykle ta warstwa jest wyraźnie odseparowana od właściwego kodu.

[2] Uzywanie aspektów
a) Techniczny kod, który nie powinien być zmieszany z kodem biznesowym
Klasycznym przykładem użycia AOP są transakcje bazodanowe w Springu. Wystarczy, że dodamy odpowiednią adnotację @Transactional na metodzie lub na klasie i już mamy włączony mechanizm transakcji. Dzięki temu unikamy dopisywania kodu, który jest odpowiedzialny za rozpoczynanie transakcji, commitowanie jej oraz rollback. W ten sposób pozbywamy się z naszego kodu biznesowego kodu, który ma charakter bardziej techniczny. Na podobnej zasadzie możemy wydzielić kod techniczny w innych miejscach.

b) Wszelkiego rodzaju loggery
Aspekty pomagają w tworzeniu mechanizmów logujących, które mogą audytować różnego rodzaju elementy aplikacji. Zwykle są to logi dotyczące użytkownika aplikacji.

Można także w ten sposób zaimplementować prosty mechanizm mierzący czasy wywołań poszczególnych metod (oczywiście są to tego lepsze narzędzia, ale na własny użytek taki mechanizm może nam wystarczyć).

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

Are singleton beans thread-safe?

A

NO

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

Why injecting beans by Field is not recommended?

A

1) It disallows immutable field declaration.
The final keyword can be used only in constructor injections. Mutability is not desired design as it allows to a change of the value in runtime.

2) Tightly coupled with dependency injection container.
The only container has the possibility to inject this class. It is not good for implementing unit tests without spring container.

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