Dependency injection Flashcards
What is dependency injection?
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.
What are the main advantages of dependency injection?
Main advantage: Have a loosely coupled system: More testable code It promotes re-usability It promotes more readable code ( thanks to config files )
What is component scanning used for ?
Spring xml setting or annotation that define which are the packages where should look over classes annotated with certain stereotype
Which are the default stereotype used in component scanning ?
@component ( first introduced in spring )
@service
@repository
@controller
Any annotation which extend these is also used
What are meta-annotations?
Are used to annotate methods and properties
How to use constructor or setter injection in xml configuration ?
The properties of a xml element can be injected or through
constructor injection
Or through the setter injection .
How to create an application context for xml configuration ?
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext
How to create an application context for java annotated configuration
AnnotationApplicationContext
What is @autowired used for?
It inject the beans depending on where is appearing. It can appear on a class field or a class method or on the constructor
What is an interface?
Is a contract for defining the main methods and constants for its implementers, developers can code different kind of implementations
Which are the main advantages of an interface?
It makes code more flexible
It supports selectable multiple inheritance
Users can call methods without knowing how the client implements the main logic
What is meant by “container”?
Spring IoC container is the core of the framework for managing , wiring and configuring beans
How do you create a container ?
ApplicationContext is used to create a container.
ApplicationContext extends BeanFactory
Which packages provide the main core components for a Spring container?
org. springframework.beans
org. springframework.context
Who is managing the lifecycle of beans?
The container is the lifecycle manager and is taking care of initialize , use and destroy beans
Which are the steps of initialize a bean?
- 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
What is the purpose of BeanFactoryPostProcessor?
This class can be used to modify the configuration metadata before instantiate a bean
Why is not ok to use BeanFactory getBean method when implementing BeanFactoryPostProcessor?
Because is violating the normal bean lifecycle
BeanPostProcessor before init when is called?
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
BeanPostProcessor after init when is called?
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
Which are the possible scopes for a bean?
- singleton
- prototype
- session
- request
- global session
- application
Which is the default scope for a bean?
If nothing is specified the default scope is singleton
Define the singleton scope
A single instance of the object will be created inside framework
Define the prototype scope
A new instance of the object will be created each time the bean is referenced