implement lightweight IOC/DI framework
- Add annotation system (@Component, @Service, @Configuration, @Bean, @Inject, @Qualifier, @Primary, @Scope, @PostConstruct, @Import, @TimiInjectApplication) - Implement BeanContext for managing bean instances and definitions - Implement BeanScanner for component scanning (file system and jar) - Implement BeanFactory with constructor injection and lifecycle support - Support @Configuration and @Bean factory methods - Support @Qualifier and @Primary for multi-implementation - Support @Scope (singleton/prototype) - Support @PostConstruct lifecycle callback - Support @Import and Module extension - Add circular dependency detection - Add dependency graph export - Add demo and test cases Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
198
README.md
198
README.md
@ -1,3 +1,199 @@
|
||||
# timi-inject
|
||||
|
||||
Java 轻量控制反转工具
|
||||
Java 轻量级控制反转与依赖注入框架。
|
||||
|
||||
## 特性
|
||||
|
||||
- 基于注解的依赖注入
|
||||
- 构造器注入为核心路径
|
||||
- 支持 @Configuration + @Bean
|
||||
- 支持 @Qualifier 与 @Primary
|
||||
- 支持 @Scope(singleton / prototype)
|
||||
- 支持 @Import 与 Module 扩展
|
||||
- 支持 @PostConstruct 生命周期回调
|
||||
- 提供依赖图导出
|
||||
|
||||
## 引入依赖
|
||||
|
||||
Maven:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.inject</groupId>
|
||||
<artifactId>timi-inject</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
1) 定义应用入口
|
||||
|
||||
```java
|
||||
@TimiInjectApplication("com.example.demo")
|
||||
public class DemoApp {
|
||||
}
|
||||
```
|
||||
|
||||
2) 定义组件与配置
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class UserService {
|
||||
public String hello() {
|
||||
return "hi";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public class DemoConfig {
|
||||
@Bean("appName")
|
||||
public String appName() {
|
||||
return "timi-inject";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3) 启动并获取 Bean
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
TimiInject inject = TimiInject.run(DemoApp.class);
|
||||
UserService userService = inject.di(UserService.class);
|
||||
String appName = inject.di("appName", String.class);
|
||||
}
|
||||
```
|
||||
|
||||
## 注解说明
|
||||
|
||||
组件注解(类上):
|
||||
|
||||
- @Component
|
||||
- @Service
|
||||
- @Controller
|
||||
- @Resources
|
||||
- @Util
|
||||
|
||||
配置注解:
|
||||
|
||||
- @Configuration: 配置类
|
||||
- @Bean: 定义 Bean 工厂方法
|
||||
- @Import: 引入外部配置或组件
|
||||
|
||||
依赖注入:
|
||||
|
||||
- 默认构造器注入
|
||||
- @Inject: 指定构造器
|
||||
- @Qualifier: 按名称限定注入
|
||||
- @Primary: 多实现时优先注入
|
||||
|
||||
作用域:
|
||||
|
||||
- @Scope(ScopeType.SINGLETON)
|
||||
- @Scope(ScopeType.PROTOTYPE)
|
||||
|
||||
生命周期:
|
||||
|
||||
- @PostConstruct: 初始化回调
|
||||
|
||||
## 多实现与限定注入
|
||||
|
||||
```java
|
||||
@Component("fastStorage")
|
||||
public class FastStorage implements Storage {}
|
||||
|
||||
@Component("defaultStorage")
|
||||
public class DefaultStorage implements Storage {}
|
||||
|
||||
@Service
|
||||
public class ReportService {
|
||||
private final Storage storage;
|
||||
|
||||
public ReportService(@Qualifier("fastStorage") Storage storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## @Primary 示例
|
||||
|
||||
```java
|
||||
@Component
|
||||
@Primary
|
||||
public class DefaultStorage implements Storage {}
|
||||
```
|
||||
|
||||
## @Configuration 与 @Bean 示例
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
@Bean
|
||||
public Clock clock() {
|
||||
return Clock.systemUTC();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## @Inject 示例
|
||||
|
||||
```java
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository repo;
|
||||
|
||||
@Inject
|
||||
public UserService(UserRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## @Import 与 Module
|
||||
|
||||
```java
|
||||
@TimiInjectApplication("com.example.demo")
|
||||
@Import({ExternalConfig.class, DemoModule.class})
|
||||
public class DemoApp {
|
||||
}
|
||||
|
||||
public class DemoModule implements Module {
|
||||
@Override
|
||||
public void configure(BeanContext context) {
|
||||
// 手动注册 BeanDefinition 或外部单例
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 手动注册 Bean
|
||||
|
||||
```java
|
||||
TimiInject inject = TimiInject.run(DemoApp.class);
|
||||
inject.ioc("runtimeConfig", new RuntimeConfig());
|
||||
```
|
||||
|
||||
## 导出依赖图
|
||||
|
||||
```java
|
||||
String graph = inject.exportDependencyGraph();
|
||||
System.out.println(graph);
|
||||
```
|
||||
|
||||
## JavaFX 示例
|
||||
|
||||
测试目录下提供独立 JavaFX demo:
|
||||
|
||||
- 入口: `src/test/java/com/imyeyu/inject/javafxdemo/FxMain.java`
|
||||
- 配置: `src/test/java/com/imyeyu/inject/javafxdemo/FxConfig.java`
|
||||
|
||||
运行方式:
|
||||
|
||||
```bash
|
||||
mvn -DskipTests javafx:run
|
||||
```
|
||||
|
||||
## 约束与建议
|
||||
|
||||
- 建议单一构造器,或显式使用 @Inject 标注
|
||||
- 多实现时必须使用 @Qualifier 或 @Primary
|
||||
- 避免在 @Bean 方法返回 null
|
||||
|
||||
Reference in New Issue
Block a user