Study notes for Pivotal Certified Spring Core Certification - v5.0
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.
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
In its most common form, an interface is a group of related methods with empty bodies. source
In my opinion, there’re some advantages:
//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)
Note: Spring Container lifecycle is different with Spring Bean lifecycle
// TODO + Code
3 steps:
@Configuration
public class ApplicationConfig {
@Bean
public DataSource dataSource{
DataSource dataSource = new DataSource();
dataSource.setUsername();
dataSource.setPassword();
...
return dataSource;
}
}
ApplicationContext context = SpringApplication.run( ApplicationConfig.class);