mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 13:24:05 -06:00
parent
6d0ccc4945
commit
7dec5524c4
64
content/Obsidian/后端/spring.md
Normal file
64
content/Obsidian/后端/spring.md
Normal file
@ -0,0 +1,64 @@
|
||||
---
|
||||
date created: 2024-10-28 18:11
|
||||
date updated: 2024-10-29 17:46
|
||||
tags:
|
||||
- spring
|
||||
- 后端
|
||||
share: "true"
|
||||
---
|
||||
|
||||
# 概述
|
||||
|
||||
## 介绍
|
||||
|
||||
Spring框架是企业使用最多的框架,没有之一。Spring是一站式框架,称之为一站式框架的原因是Spring可以整合其他框架。
|
||||

|
||||
|
||||
要学习Spring的内容如下:
|
||||
|
||||
- IoC:控制反转
|
||||
- DI:[[../编程模型及方法/依赖注入|依赖注入]]
|
||||
- Spring AOP:面向切面编程技术,为Spring事务管理打下基础。
|
||||
- Spring Transaction management:Spring事务管理。
|
||||
- Spring Web MVC(不包含在本课程内,后面单独学习):简称Spring MVC框架,用来简化JavaWEB开发,当使用Spring MVC框架后,就不用再编写Servlet了。也就不再需要itcast-tools工具中BaseServlet类了。
|
||||
- Spring与其他框架整合:因为我们只学习过MyBatis框架,所以当前我们只学习Spring整合MyBatis框架。
|
||||
|
||||
# 原理
|
||||
|
||||
## IoC
|
||||
|
||||
Spring IoC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,它是Spring框架的核心。
|
||||
|
||||
Spring IOC用于==管理Java对象之间的依赖关系==,==将对象的创建、组装、管理交给框架来完成==,而不是由开发者手动完成。
|
||||
|
||||
IOC不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合、更优良的程序。
|
||||
|
||||
传统应用程序都是由我们在类内部主动创建依赖对象,从而导致**类与类之间高耦合**,难于测试,有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,如下图所示:
|
||||
|
||||
Spring IoC的核心如下:
|
||||
|
||||
- 工厂负责对象生命周期的管理;(spring管理创建与销毁)
|
||||
- 对象的依赖由工厂完成注入。(spring维护对象间关系)
|
||||
|
||||
Spring提出了对象工厂的概念,由Spring工厂来管理对象的生命周期。所谓对象生命周期指的是==从对象的创建一直到对象的销毁都由Spring来管理==。我们无需再自己new对象,而是从Spring工厂中获取需要的对象。甚至**对象的依赖也由工厂来注入,无需手动注入依赖**。
|
||||
|
||||
Spring工厂是ApplicationContext接口,通常我们使用的是AnnotationConfigApplicationContext类。其中Spring工厂内部是通过Map类型来维护的。
|
||||
|
||||
| Key | value |
|
||||
| --------------------------------------- | ------------- |
|
||||
| “userDao1” | UserDao实例 |
|
||||
| “userService1” | UserService实例 |
|
||||
| ... | ... |
|
||||
| 当我们需要获取工厂中的实例时,只需要调用工厂的getBean(“id”)即可。 | |
|
||||
| ```java | |
|
||||
| @Test public void test3() { | |
|
||||
|
||||
```java
|
||||
|
||||
@Test public void test3() {
|
||||
AnnotationConfigApplicationContext context = ...
|
||||
UserDao userDao = (UserDao) context.getBean("userDao1");
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
date created: 2024-10-29 11:28
|
||||
date updated: 2024-10-29 11:40
|
||||
date updated: 2024-10-29 17:45
|
||||
tags:
|
||||
- 设计模式
|
||||
share: "true"
|
||||
@ -111,3 +111,77 @@ int main() {
|
||||
在这个例子中,`Dependency` 结构体定义了一个打印消息的==函数指针==。`print_message` 函数实现了这个接口。`use_dependency` 函数接受一个`Dependency`结构体指针作为参数,并调用其`print_message`方法。在`main`函数中,我们创建了一个`Dependency`实例并设置了它的`print_message`方法指针指向`print_message`函数的实现。
|
||||
|
||||
这个例子展示了如何在不修改`use_dependency`函数代码的情况下,通过结构体和函数指针来动态注入依赖。虽然这不是面向对象编程中的依赖注入,但在C语言中可以通过这种方式模拟依赖注入。
|
||||
|
||||
# C++简单实现依赖注入
|
||||
|
||||
在C++中实现依赖注入的一种简单方式是使用工厂模式和配置文件。以下是一个简单的例子:
|
||||
|
||||
```c++
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
// 抽象基类
|
||||
class BaseClass {
|
||||
public:
|
||||
virtual void Show() = 0;
|
||||
virtual ~BaseClass() = default;
|
||||
};
|
||||
|
||||
// 实现类A
|
||||
class ClassA : public BaseClass {
|
||||
public:
|
||||
void Show() override {
|
||||
std::cout << "Class A" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
// 实现类B
|
||||
class ClassB : public BaseClass {
|
||||
public:
|
||||
void Show() override {
|
||||
std::cout << "Class B" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
// 工厂类
|
||||
class Factory {
|
||||
public:
|
||||
BaseClass* Create(const std::string& type) {
|
||||
if (creators.find(type) != creators.end()) {
|
||||
return creators[type]();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Register(const std::string& type, std::function<BaseClass*()> creator) {
|
||||
creators[type] = creator;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, std::function<BaseClass*()>> creators;
|
||||
};
|
||||
|
||||
// 使用工厂
|
||||
int main() {
|
||||
Factory factory;
|
||||
|
||||
// 注册类
|
||||
factory.Register("A", []() { return new ClassA(); });
|
||||
factory.Register("B", []() { return new ClassB(); });
|
||||
|
||||
// 创建对象
|
||||
BaseClass* obj = factory.Create("A");
|
||||
if (obj) {
|
||||
obj->Show();
|
||||
delete obj;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
这段代码定义了一个基类`BaseClass`和两个实现类`ClassA`和`ClassB`。`Factory`类负责创建对象,通过`Register`方法将类型与创建对象的函数绑定,通过`Create`方法根据类型创建对象。
|
||||
|
||||
在`main`函数中,我们注册了两个实现类,并根据提供的类型字符串创建了一个对象。这个例子展示了依赖注入的简单实现,但在实际应用中可能需要考虑内存管理、多线程和异常处理等问题。
|
||||
|
||||
Loading…
Reference in New Issue
Block a user