项目作者: bachnxhedspi

项目描述 :
Study notes for Pivotal Certified Spring Core Certification - v5.0
高级语言: Java
项目地址: git://github.com/bachnxhedspi/spring-core-cert-notes-5.0.git
创建时间: 2018-08-08T13:11:59Z
项目社区:https://github.com/bachnxhedspi/spring-core-cert-notes-5.0

开源协议:

下载


Core Spring 5.0 Study Notes

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.

Container, Dependency, and IOC

What is dependency injection and what are the advantages?

  • 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

What is a pattern? What is an anti-pattern. Is dependency injection a pattern?

  • 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.

What is an interface and what are the advantages of making use of them in Java?

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

What is meant by “application-context?

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)

The Spring IoC container

What is the concept of a “container” and what is its lifecycle?

  • 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

How are you going to create a new instance of an ApplicationContext?

// TODO + Code

3 steps:

  • Configure for class.
  • Configuration Instructions with @Configuration annotation
    1. @Configuration
    2. public class ApplicationConfig {
    3. @Bean
    4. public DataSource dataSource{
    5. DataSource dataSource = new DataSource();
    6. dataSource.setUsername();
    7. dataSource.setPassword();
    8. ...
    9. return dataSource;
    10. }
    11. }
  • Creating and Using the Application
  1. ApplicationContext context = SpringApplication.run( ApplicationConfig.class);

Can you describe the lifecycle of a Spring Bean in an ApplicationContext?