This is the answer I prepared for the question list of the Pivotal's Certification Study Guide; inspired by Vojtech Ruzicka's Exam Notes – Pivotal Certified Spring Professional.
-
Dependency inversion: a principle in SOLID about decoupling the high-level and low-level modules from each other.
-
Inversion of Control is a design pattern follow Dependency Inversion. There are many implementation such as Event, Delegator, Dependency Injection... In Spring, IoC is also known as Dependency Injection.
-
Dependency injection (DI): a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
Example: a Car class is dependent on the Engine class. In order to create a Car object, we need to create the Engine of the car.
//TODO + Code
Further reading: a series about SOLID, Good series about DI (Vietnamese), my friend's blog
- Pattern: a general solution to a commonly occuring problem within a given context. Example: Singleton - each clas has only 1 instance; Iterator - a way to access each element of an aggregate object without exposing its underlying representation (foreach in Java, PHP,...).
- Anti-pattern: a common responses to recurring problem that is ineffective and risks. Example: Hard Code configuration information instead of provide them in a config file.
- Dependency Injection is a design pattern. Be noticed that it was not mentioned in the Gang of Four book.
In its most common form, an interface is a group of related methods with empty bodies. source
In my opinion, there're some advantages:
- A contract for a sets of class to implement a sets of method.
- To implement Abstraction.
- Easier to implement Dependency Injection.
- ...
//TODO
The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans.
The following diagram is a high-level view of how Spring works. Your application classes are combined with configuration metadata so that after the ApplicationContext is created and initialized, you have a fully configured and executable system or application. (Source)
- A Spring IoC container manages one or more beans.
- Container runs through 3 phases: Initialization (Create Spring Beans, Inject dependencies), Usage, Destruction (release Beans for Garbage Collection)
Note: Spring Container lifecycle is different with Spring Bean lifecycle
// TODO + Code
3 steps:
- Configure for class.
- Configuration Instructions with @Configuration annotation
@Configuration
public class ApplicationConfig {
@Bean
public DataSource dataSource{
DataSource dataSource = new DataSource();
dataSource.setUsername();
dataSource.setPassword();
...
return dataSource;
}
}
- Creating and Using the Application
ApplicationContext context = SpringApplication.run( ApplicationConfig.class);