
简单来说就是当自己必须一个对象的之后不需要自己手动去new一个,而是由其它容器来给你提供;Spring里面就是IOC容器。
例如:
在Spring里面常常必须在Service这个装配一个Dao,一般是使用@Autowired注解:类似如下

public Class ServiceImpl{
@Autowired
Dao dao;
public void getData(){
dao.getData();
}
在这里未初始化Dao直接使用是会报出空指针异常的,那么在Spring里面的做法就是通过反射来将必须的类给你加载进来。
首先写两个注解模拟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();
}

下面是借助反射来直接注入:
首先遍历指定的包名:这一步先省略spring ioc原理 反射spring ioc原理 反射,
首先是制定一个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容器比这个超强太多了,比如必须考量线程安全,以及各类的细节问题
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-138285-1.html
但怎么玩你们说了就不算了
如果说那也算秀的话
定了再发