-
Design Pattern
- Singleton Design Pattern
- Factory Design Pattern
- Factory Method Design Pattern
- Abstract Factory Design Pattern
- Builder Design Pattern
- Prototype Design Pattern
- Object Pool Design Pattern
- Chain of Responsibility Design Pattern
- Command Design Pattern
- Interpreter Design Pattern
- Iterator Design Pattern
- Mediator Design Pattern
- Memento Design Pattern
- Observer Design Pattern
- Observer Design Pattern - Xử Lý Exception
- Strategy Design Pattern
- Template Method Design Pattern
- Visitor Design Pattern
- Null Object Design Pattern
- Adapter Design Pattern
- Bridge Design Pattern
- Composite Design Pattern
- Decorator Design Pattern
- Flyweight Design Pattern
- Proxy Design Pattern
- S.O.L.I.D
- Clean code
- Lập trình socket
- Java Core
- Multi-Thread
- Spring
- Java Web
- Memory Caching
- Message Queue
- DevOps
- Xây dựng một nền tảng
- MongoDB
- MySQL timestamp
- Properties vs yaml
- Kotlin
- Lập Trình Machine Learning với PyTorch
- Mã Nguồn Mở
- Ezy HTTP
- Free Chat
- Một số kinh nghiệm với Git
- Review cho đồng nghiệp!
- Setup Dev Environment
- Hello World
- Create a Server Project
- Handle Client Requests
- Using ezyfox-server-csharp-client
- Using ezyfox-es6-client
- Client React.js Interaction
- Build And Deploy In Local

Trái tim của Spring!
ApplicationContext là nơi chứa tất cả cấu hình, các bean (các đối tượng singleton và prototype) của chương trình mà nó thu thập được hoặc do chúng ta chủ động set vào, bên cạnh đó nó cũng publish các sự kiện cho chúng ta trong quá trình khởi tạo để cho phép chúng ta làm một số việc chúng ta muốn, ví dụ ContextStartedEvent hay ContextRefreshedEvent.
AnnotationConfigApplicationContext
Spring cung cấp ApplicationContext là một interface và chúng ta sẽ phải lựa chọn lớp cài đặt phù hợp cho mình, và với các tính năng mạnh mẽ của Java Annotation chúng ta thường lựa chọn AnnotationConfigApplicationContext.
Khởi tạo ApplicationContext
Để khởi toạ một application context tương đối đơn giản, chúng ta chỉ cần truyền các tên gói (package) mà có chứa các lớp component (các lớp được annotation với @Configuration, @Component, @Service, @Controller ...) vào cho đối tượng AnnotationConfigApplicationContext
và cuối cùng là gọi hàm refresh
.
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.tvd12.example.spring_core.config");
context.scan("com.tvd12.example.spring_core.repo");
context.scan("com.tvd12.example.spring_core.service");
context.refresh();
Sau khi gọi context.refresh()
thì tất cả các đối tượng sẽ được khởi tạo, tự động set các thuộc tính do ApplicationContext
quản lý.
Sử dụng ApplicationContext
Để lấy một đối tượng (bean) từ ApplicationContext
, chúng ta chỉ cần gọi hàm getBean
như sau:
BookService bookService = context.getBean(BookService.class);
Để import file cấu hình vào ApplicationContext
chúng ta cần sử dụng annotation @PropertySource
và để lấy một property từ ApplicationContext, chúng ta có thể làm như sau:
Environment environment = context.getEnvironment();
String world = environment.getProperty("hello");
Ví dụ đầy đủ bạn có thể tham khảo tạo Github
Tổng kết
Ở thời điểm của bài viết này thì có lẽ anh em đang dùng spring-boot hết cả rồi, tuy nhiên trái tim là phần quan trọng nhất chúng ta, không biết có lẽ là sự thiếu sót lớn.
Tham khảo
-
Design Pattern
- Singleton Design Pattern
- Factory Design Pattern
- Factory Method Design Pattern
- Abstract Factory Design Pattern
- Builder Design Pattern
- Prototype Design Pattern
- Object Pool Design Pattern
- Chain of Responsibility Design Pattern
- Command Design Pattern
- Interpreter Design Pattern
- Iterator Design Pattern
- Mediator Design Pattern
- Memento Design Pattern
- Observer Design Pattern
- Observer Design Pattern - Xử Lý Exception
- Strategy Design Pattern
- Template Method Design Pattern
- Visitor Design Pattern
- Null Object Design Pattern
- Adapter Design Pattern
- Bridge Design Pattern
- Composite Design Pattern
- Decorator Design Pattern
- Flyweight Design Pattern
- Proxy Design Pattern
- S.O.L.I.D
- Clean code
- Lập trình socket
- Java Core
- Multi-Thread
- Spring
- Java Web
- Memory Caching
- Message Queue
- DevOps
- Xây dựng một nền tảng
- MongoDB
- MySQL timestamp
- Properties vs yaml
- Kotlin
- Lập Trình Machine Learning với PyTorch
- Mã Nguồn Mở
- Ezy HTTP
- Free Chat
- Một số kinh nghiệm với Git
- Review cho đồng nghiệp!
- Setup Dev Environment
- Hello World
- Create a Server Project
- Handle Client Requests
- Using ezyfox-server-csharp-client
- Using ezyfox-es6-client
- Client React.js Interaction
- Build And Deploy In Local