控制反转(InversionofControl,缩写为IoC)
简单来说就是当自己需要一个对象的时候不需要自己手动去new一个,而是由其他容器来帮你提供;Spring里面就是IOC容器。
例如:
在Spring里面经常需要在Service这个装配一个Dao,一般是使用@Autowired注解:类似如下
?
1
2
3
4
5
6
7
|
public Class ServiceImpl{
@Autowired
Dao dao;
public void getData(){
dao.getData();
}
|
在这里未初始化Dao直接使用是会报出空指针异常的,那么在Spring里面的做法就是通过反射来将需要的类帮你加载进来。
下面是一个例子模拟了Spring的DI和IOC
首先写两个注解模拟Spring的注解:
?
1
2
3
4
5
6
7
8
9
10
11
|
Entity注解代表的是Spring的 @Service
@Target (ElementType.TYPE) // 类
@Retention (RetentionPolicy.RUNTIME)
public @interface Entity {
}
代表的是Spring里面的 @Autowrid
@Target (ElementType.FIELD) //描述方法的
@Retention (RetentionPolicy.RUNTIME) // 仅运行时保留
public @interface Resources {
}
|
当注解建立完成之后再建立两个类:
Rain类代表的是需要从其他地方获取天气数据(数据库或者服务器)
?
1
2
3
4
5
|
public class Rain {
public void rain(){
System.out.println( "正在下雨" ); // 为了方便直接写了
}
}
|
Weather类代表的是获取到的天气数据
?
1
2
3
4
5
6
7
|
@Entity
public class Weather {
@Resources
Rain rain; // 这里在后面通过反射直接注入rain
public void weather_rain() {
rain.rain();
}
|
下面是通过反射来直接注入:
首先遍历指定的包名:这一步先省略,
首先是建立一个List模拟Spring的bean容器,即将已经装初始化好的带有Entity注解的类全部初始化
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class Weather_reflec {
List<Object> objectList ;
// 模拟Spring容器
public Weather_reflec() {
objectList= new ArrayList<Object>();
}
// 在这里其实最好的做法是先找出带有注解的类,判断带有Entity注解再传入。但是为了方便直接省略了
public void get_ref(Object object) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<?> clazz =object.getClass();
if (clazz.isAnnotationPresent(Entity. class )){
//判断是否带有Entity注解
Field[] fields =clazz.getDeclaredFields();
//获取其变量
for (Field field :fields){
if (field.isAnnotationPresent(Resources. class )){
//判断是否需要注入
Class<?> rainClass=Class.forName(field.getType().getName(), false ,Thread.currentThread().getContextClassLoader());
// 这里先将Rain类加载
field.set(object,rainClass.newInstance());
//赋给rain
objectList.add(object);
//最后将已将赋值后的Weather保存进容器
}
}
}
}
public List<Object> returnList(){
return objectList;
//返回容器方便以后使用
}
|
最后也就是模拟Controller里面直接使用的
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class WeatherPrediction {
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
WeatherPrediction weatherPrediction = new WeatherPrediction();
Weather weather =(Weather)weatherPrediction.springDo();
weather.weather_rain();
// 这里如果是普通调用会报空指针异常,而容器却为其将rain这个变量赋值了,所以可以正常输出
}
/*
模拟Spring启动过程,这一步其实可以单独写一个类,这一步是容器该做的,而我们并不需要去管
*/
public Object springDo() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Weather_reflec weather_reflec = new Weather_reflec();
// 启动的时候就需要加载的
Weather weather = new Weather();
//扫描类注解后new操作然后进行下一步
weather_reflec.get_ref(weather);
// 将其类里面的变量进行new操作并放入容器
Object object =weather_reflec.returnList().get( 0 );
return object;
}
运行后输出:正在下雨
|
在WeatherPrediction里面并没有对Rain进行一个new操作但是却可以使用,这应该是最简单的一个模拟Spring的IOC例子了,当然Spring的IOC容器比这个强大太多了,比如需要考虑线程安全,以及各种的细节问题
总结
以上就是本文关于简单实现Spring的IOC原理详解的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:https://segmentfault.com/a/1190000009139271
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 22
-
2025-05-25 40
-
2025-05-29 17
-
2025-05-27 85
-
2025-05-25 71
热门评论