当前位置: 移动技术网 > IT编程>开发语言>Java > 简单实现Spring的IOC原理详解

简单实现Spring的IOC原理详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

控制反转(inversionofcontrol,缩写为ioc)

简单来说就是当自己需要一个对象的时候不需要自己手动去new一个,而是由其他容器来帮你提供;spring里面就是ioc容器。

例如:

在spring里面经常需要在service这个装配一个dao,一般是使用@autowired注解:类似如下

 public class serviceimpl{
  @autowired
   dao dao;
   
  public void getdata(){
    dao.getdata();
  }

在这里未初始化dao直接使用是会报出空指针异常的,那么在spring里面的做法就是通过反射来将需要的类帮你加载进来。

下面是一个例子模拟了spring的di和ioc

首先写两个注解模拟spring的注解:

entity注解代表的是spring的@service
@target(elementtype.type) // 类
@retention(retentionpolicy.runtime)
public @interface entity {
}

代表的是spring里面的@autowrid
@target(elementtype.field) //描述方法的
@retention(retentionpolicy.runtime) // 仅运行时保留
public @interface resources {
}

当注解建立完成之后再建立两个类:
rain类代表的是需要从其他地方获取天气数据(数据库或者服务器)

public class rain {
  public void rain(){
    system.out.println("正在下雨"); // 为了方便直接写了
  }
}

weather类代表的是获取到的天气数据

@entity
public class weather {
  @resources
  rain rain; // 这里在后面通过反射直接注入rain
  public void weather_rain() {
    rain.rain();
  }

下面是通过反射来直接注入:

首先遍历指定的包名:这一步先省略,

首先是建立一个list模拟spring的bean容器,即将已经装初始化好的带有entity注解的类全部初始化

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里面直接使用的

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原理详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

spring ioc原理详解

简单理解spring之ioc和aop及代码示例

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网