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