Dependency injection Flashcards

1
Q

What is dependency injection?

A

Dependency Injection is a design pattern which Spring implemented through it’s IoC container to create and implements object dependency using configuration files. The objects can be injected through constructor or setter injection.

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

What are the main advantages of dependency injection?

A
Main advantage:
Have a loosely coupled system:
More testable code
It promotes re-usability 
It promotes more readable code ( thanks to config files )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is component scanning used for ?

A

Spring xml setting or annotation that define which are the packages where should look over classes annotated with certain stereotype

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

Which are the default stereotype used in component scanning ?

A

@component ( first introduced in spring )
@service
@repository
@controller

Any annotation which extend these is also used

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

What are meta-annotations?

A

Are used to annotate methods and properties

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

How to use constructor or setter injection in xml configuration ?

A

The properties of a xml element can be injected or through

constructor injection
Or through the setter injection .

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

How to create an application context for xml configuration ?

A

ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext

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

How to create an application context for java annotated configuration

A

AnnotationApplicationContext

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

What is @autowired used for?

A

It inject the beans depending on where is appearing. It can appear on a class field or a class method or on the constructor

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

What is an interface?

A

Is a contract for defining the main methods and constants for its implementers, developers can code different kind of implementations

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

Which are the main advantages of an interface?

A

It makes code more flexible

It supports selectable multiple inheritance

Users can call methods without knowing how the client implements the main logic

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

What is meant by “container”?

A

Spring IoC container is the core of the framework for managing , wiring and configuring beans

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

How do you create a container ?

A

ApplicationContext is used to create a container.

ApplicationContext extends BeanFactory

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

Which packages provide the main core components for a Spring container?

A

org. springframework.beans

org. springframework.context

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

Who is managing the lifecycle of beans?

A

The container is the lifecycle manager and is taking care of initialize , use and destroy beans

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

Which are the steps of initialize a bean?

A
  • beans are loaded
  • post process beans definition ( load into beanfactory )
  • each bean is created
  • setters are called
  • bean post processor ( before init )
  • init method is called
  • bean post processor ( after init )
  • bean is ready for use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the purpose of BeanFactoryPostProcessor?

A

This class can be used to modify the configuration metadata before instantiate a bean

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

Why is not ok to use BeanFactory getBean method when implementing BeanFactoryPostProcessor?

A

Because is violating the normal bean lifecycle

19
Q

BeanPostProcessor before init when is called?

A

Every registered BeanPostProcessor is called in order before @PostConstruct ( init method ) method if one is returning null the process stops there and nothing else is called

20
Q

BeanPostProcessor after init when is called?

A

Every registered implementation is called in order starting after the call of @PostConstruct method ( init method ) and if one return null the process stops there

21
Q

Which are the possible scopes for a bean?

A
  • singleton
  • prototype
  • session
  • request
  • global session
  • application
22
Q

Which is the default scope for a bean?

A

If nothing is specified the default scope is singleton

23
Q

Define the singleton scope

A

A single instance of the object will be created inside framework

24
Q

Define the prototype scope

A

A new instance of the object will be created each time the bean is referenced

25
Q

Define the session scope

A

It is used in web environment and a new instance will be created per user web HTTP session

26
Q

Define the request scope

A

It is used in web environment and a new instance will be created per each request

27
Q

Define the global session scope

A

Same as session and used in web applications, typically used in portlet context

28
Q

Define the application scope

A

It is used in web applications and Scopes a single bean definition in a ServletContext

29
Q

Which are the short versions of constructor injection in xml configuration ?

A

For constructor injection we have

  • c:[name_property]-ref for bean reference
  • c:[name_property] for scalar values
  • c:_[pos] for scalar value (ex: c:_0)
  • c:_[pos]-ref for bean references
30
Q

How to import the short version of constructor injection?

A

In xml beans element an xmlns:c attribute should be created with value http://www.springframework.org/schema/c

31
Q

Which are the short versions of setter injection in xml configuration ?

A

For setter injection we have

  • p:[name_property]-ref for bean references
  • p:[name_property] for scalar values
32
Q

How to import the short version of setter injection?

A

In xml beans element an xmlns:p attribute should be created with value http://www.springframework.org/schema/p

33
Q

What is an initialization method ?

A

Is a method which is called after all the properties are injected and the bean initialized

34
Q

How you can declare an initialization method for a bean in Spring?

A

Through the xml configuration attribute init-method in the bean element

Through the @PostConstruct annotation in the bean class if annotations are enabled or AnnotationApplicationContext is initialized

Through the implementation of the InitializingBean interface

35
Q

What is a destroy method?

A

A destroy method is used to release the resources before the class is destroyed

36
Q

How a destroy method is declared in Spring?

A
  • by using the @PreDestroy annotation on method if annotations are enabled
  • by using the destroy-method attribute on the bean element
  • by implementing the DisposableBean interface
37
Q

When the destroy method is called ?

A

After the context.close method is called and beans and resources are released

38
Q

What is happening if I inject 1977/10/58 in a date property ?

A

Spring will automatically convert the date and will add the days to the next month giving 27 nov 1977 like a date

39
Q

Are beans lazy or eagerly instantiated by default ?

A

All the beans are eagerly instantiated by default. This consume more time at the startup but ensure that once all the beans are loaded no errors will appear later.

40
Q

How you can set a bean to load in lazy mode?

A

In order to load a bean in a lazy init way the attribute on the bean lazy-init should be true. If this attribute is set to default than the default-lazy-init attribute will be checked.

41
Q

How does the @Qualifier annotation complement the use of @Autowired?

A

is helping @Autowired by configuring the id of the bean that should be auto wired if more than one of the same type exist in the configuration file

42
Q

How can a list or set be injected ?

A

In order to inject a list or a set the or element should be used. Inside the list or set than ref or bean or null element should be used

43
Q

How do you inject a map in a setter property ?

A

In order to inject the map the right element is for each element of the map exist the element or if the element is a reference

44
Q

How to use Spring until for map and lists?

A

First of all xmlns:util should be created and spring-until.xsd imported after the follow code can be written