tClass) {
Object object = getBean(beanName);
return (T)object;
}
}
```
其中 ClassUtils 是基于 class 的反射工具类,详情见 [ClassUtils.java](https://github.com/houbb/ioc/blob/release_0.0.1/src/main/java/com/github/houbb/ioc/util/ClassUtils.java)
## JsonApplicationContext
基于 json 配置文件实现的基本实现,使用方式见开始种的例子代码。
- JsonApplicationContext.java
```java
/**
* JSON 应用上下文
* @author binbin.hou
* @since 0.0.1
*/
public class JsonApplicationContext extends DefaultBeanFactory {
/**
* 文件名称
* @since 0.0.1
*/
private final String fileName;
public JsonApplicationContext(String fileName) {
this.fileName = fileName;
// 初始化配置
this.init();
}
/**
* 初始化配置相关信息
*
*
* new TypeReference>(){}
*
*
* 读取文件:https://blog.csdn.net/feeltouch/article/details/83796764
* @since 0.0.1
*/
private void init() {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
final String jsonConfig = FileUtil.getFileContent(is);
List beanDefinitions = JsonBs.deserializeArray(jsonConfig, DefaultBeanDefinition.class);
if(CollectionUtil.isNotEmpty(beanDefinitions)) {
for (BeanDefinition beanDefinition : beanDefinitions) {
super.registerBeanDefinition(beanDefinition.getName(), beanDefinition);
}
}
}
}
```
## 小结
至此,一个最基本的 spring ioc 就基本实现了。
如果你想继续学习,可以分别参考以下代码分支。
# 分支说明
[v0.0.1-BeanFactory 基本实现](https://github.com/houbb/ioc/tree/release_0.0.1)
[v0.0.2-ListBeanFactory 基本实现](https://github.com/houbb/ioc/tree/release_0.0.2)
[v0.0.3-单例和延迟加载](https://github.com/houbb/ioc/tree/release_0.0.3)
[v0.0.4-初始化和销毁方法](https://github.com/houbb/ioc/tree/release_0.0.4)
[v0.0.5-RespCode 添加和代码优化](https://github.com/houbb/ioc/tree/release_0.0.5)
[v0.0.6-构造器和 factoryMethod 新建对象](https://github.com/houbb/ioc/tree/release_0.0.6)
[v0.0.7-property 属性设置](https://github.com/houbb/ioc/tree/release_0.0.7)
[v0.0.8-Aware 监听器及 PostProcessor](https://github.com/houbb/ioc/tree/release_0.0.8)
[v0.0.9-Parent 属性继承](https://github.com/houbb/ioc/tree/release_0.0.9)
[v0.1.0-循环依赖检测](https://github.com/houbb/ioc/tree/release_0.1.0)
[v0.1.1-@Configuration-java 代码配置](https://github.com/houbb/ioc/tree/release_0.1.1)
[v0.1.2-@Bean-java 对象定义](https://github.com/houbb/ioc/tree/release_0.1.2)
[v0.1.3-@Lazy-@Scope-java 对象属性配置](https://github.com/houbb/ioc/tree/release_0.1.3)
[v0.1.4-@Import 配置导入](https://github.com/houbb/ioc/tree/release_0.1.4)
[v0.1.5-@Bean 参数构造以及 @Description](https://github.com/houbb/ioc/tree/release_0.1.5)
[v0.1.6-@Autowired 自动装配注解支持](https://github.com/houbb/ioc/tree/release_0.1.6)
[v0.1.7-@Primary 指定优先级注解](https://github.com/houbb/ioc/tree/release_0.1.7)
[v0.1.8-@Conditional 条件注解支持](https://github.com/houbb/ioc/tree/release_0.1.8)
[v0.1.9-Environment 和 @Profile 实现](https://github.com/houbb/ioc/tree/release_0.1.9)
[v0.1.10-Property 配置文件相关和 @Value/@PropertyResource 实现](https://github.com/houbb/ioc/tree/release_0.1.10)
[v0.1.11-@ComponentScan 文件包扫描支持](https://github.com/houbb/ioc/tree/release_0.1.11)
# 拓展阅读
[Java IOC-00-ioc 是什么](https://houbb.github.io/2019/11/06/java-ioc-01-overview)