-
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

Vén bức màn bí ẩn!
Có lẽ từ 2016 trở lại đây thì đã phần mọi người hay sử dụng Spring Boot vì sự tiện lợi của nó, nhưng có lẽ nhiều người trong chúng ta vẫn còn cảm thấy nó bí ẩn (Ồ, tại sao nó lại tự khởi tạo được 1 cái web server rồi chạy lên nhỉ?). Và đến thời điểm này có lẽ nhiều người vẫn còn lầm tưởng Spring Boot chính là 1 web server.
Giới thiệu

Vậy Spring Boot là gì? Spring Boot thực tế chỉ là một tầng cao hơn của spring-context, nó sử dụng module spring-boot-autoconfig
để đi quét qua tất cả các lớp có annotation @Configuration
, các lớp này sẽ chịu trách nhiệm load các lớp có trong classpath của chương trình để khởi tạo, nếu khởi tạo thành công, nó sẽ đưa bean khởi tạo được vào ApplicationContext, Nó kiểu thế này:
try {
RestTemplateAutoConfiguration configuration = new RestTemplateAutoConfiguration();
appContext.add(configuration.restTemplateBuilder(...));
}
catch (Throwable e) {
// bỏ qua
}
Chúng ta có thể thấy rốt cuộc là Spring Boot cũng chỉ là một cách thức để tìm kiếm các bean mà thôi. Và có lẽ Spring Boot cũng là thứ dễ tạo ra nhất trong bộ thư viện của Spring
Ví dụ
Để sử dụng spring boot rất đơn giản, chúng ta chỉ cần annotate lớp chứa hàm main với annotation @SpringBootConfiguration
là xong
@SpringBootConfiguration
public class MyApplicationStartup {
public static void main(String[] args) {
ApplicationContext appContext =
SpringApplication.run(MyApplicationStartup.class, args);
}
}
}
Vậy có một câu hỏi là, giả sử trong classpath của tôi có rất nhiều thư viện, và tôi không muốn Spring Boot load nó lên thì làm thế nào? Đây thực sự là vấn đề nhức nhối và đôi khi giải quyết thật khó khăn, nhưng may mắn Spring Boot cung cấp cho chúng ta annotation @EnableAutoConfiguration
, chúng ta có thể sử dụng thế này.
@EnableAutoConfiguration(
exclude = {
RestTemplateAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
}
)
Kết luận
Spring Boot rốt cuộc cũng chỉ là một công cụ giúp chúng ta tự động tìm kiếm được các bean và các lớp cấu hình, sử dụng nó sẽ giúp chúng ta đỡ phải tạo lại các lớp Configuration, đặc biệt là các lớp Configuration khó như cho tomcat web server chẳng hạn. Tuy nhiên trong nhiều trường hợp chúng ta cũng sử dụng nhiều loại thư viện khác nữa và không có cách nào để tránh được conflict (mặc dù đã dùng @EnableAutoConfiguration
để exclude) thì bỏ Spring Boot đi cũng không sao
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