今日的开发零散知识点:注解、依赖注入、中间件、跨域
注解
给代码(编译)、开发者看的注释
python注解:
1
2
| def greet(name: str) -> str:
return f"Hello, {name}"
|
java注解:
1
2
3
4
5
6
7
8
9
10
11
12
| @Override
public String toString() {
return "Example";
}
@Deprecated
public void oldMethod() {
// 已过时的方法
}
@SuppressWarnings("unchecked")
List<String> list = new ArrayList();
|
依赖注入
不用自己“new”对象,告诉框架“我需要什么”,框架会自动给你“注入”。
python依赖注入:
1
2
3
4
5
6
7
| class UserService:
def __init__(self, database_connection):
self.db = database_connection
# 使用时
db_conn = DatabaseConnection()
user_service = UserService(db_conn)
|
Python的FastAPI里面有很多依赖注入方式(# TODO)
java依赖注入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| @Component
public class UserService {
private final UserRepository userRepository;
// 构造函数注入
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
// 使用注解自动注入
@Service
public class UserController {
@Autowired
private UserService userService;
}
|
中间件
客户端和服务端(请求方和提供方)之间提供和支撑数据交互的中间层
跨域
域名、子域名、端口、ip,这些会引起跨域问题,就是访问不到对应的资源项目