服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring boot实现一个简单的ioc(2)

Spring boot实现一个简单的ioc(2)

2020-09-21 15:34clayanddev Java教程

这篇文章主要为大家详细介绍了Spring boot实现一个简单ioc的第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

跳过废话,直接看正文
仿照spring-boot的项目结构以及部分注解,写一个简单的ioc容器。
测试代码完成后,便正式开始这个ioc容器的开发工作。

正文

项目结构

Spring boot实现一个简单的ioc(2)

实际上三四个类完全能搞定这个简单的ioc容器,但是出于可扩展性的考虑,还是写了不少的类。
因篇幅限制,接下来只将几个最重要的类的代码贴出来并加以说明,完整的代码请直接参考https://github.com/clayandgithub/simple-ioc

SimpleAutowired

代码

?
1
2
3
4
5
6
7
8
9
10
import java.lang.annotation.*;
 
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SimpleAutowired {
 boolean required() default true;
 
 String value() default ""; // this field is moved from @Qualifier to here for simplicity
}

说明

@SimpleAutowired的作用是用于注解需要自动装配的字段。
此类和spring的@Autowired的作用类似。但又有以下两个区别:
- @SimpleAutowired只能作用于类字段,而不能作用于方法(这样实现起来相对简单些,不会用到aop)
- @SimpleAutowired中包括了required(是否一定需要装配)和value(要装配的bean的名字)两个字段,实际上是将spring中的@Autowired以及Qualifier的功能简单地融合到了一起

SimpleBean

代码

?
1
2
3
4
5
6
7
8
import java.lang.annotation.*;
 
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SimpleBean {
 String value() default "";
}

说明

@SimpleBean作用于方法,根据方法返回值来生成一个bean,对应spring中的@Bean
用value来设置要生成的bean的名字

SimpleComponent

代码

?
1
2
3
4
5
6
7
8
import java.lang.annotation.*;
 
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SimpleBean {
 String value() default "";
}

说明

@SimpleComponent作用于类,ioc容器会为每一个拥有@SimpleComponent的类生成一个bean,对应spring中的@Component。特殊说明,为了简单起见,@SimpleComponent注解的类必须拥有一个无参构造函数,否则无法生成该类的实例,这个在之后的SimpleAppliationContext中的processSingleClass方法中会有说明。

SimpleIocBootApplication

代码

?
1
2
3
4
5
6
7
8
import java.lang.annotation.*;
 
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SimpleIocBootApplication {
 String[] basePackages() default {};
}

说明

@SimpleIocBootApplication作用于应用的入口类。
这个启动模式是照搬了spring-boot的启动模式,将启动任务委托给SimpleIocApplication来完成。ioc容器将根据注解@SimpleIocBootApplication的相关配置自动扫描相应的package,生成beans并完成自动装配。(如果没有配置,默认扫描入口类(测试程序中的SampleApplication)所在的package及其子package)

以上就是这个ioc容器所提供的所有注解,接下来讲解ioc容器的扫描和装配过程的实现。

SimpleIocApplication

代码

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import com.clayoverwind.simpleioc.context.*;
import com.clayoverwind.simpleioc.util.LogUtil;
 
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Logger;
 
 
public class SimpleIocApplication {
 private Class<?> applicationEntryClass;
 
 private ApplicationContext applicationContext;
 
 private final Logger LOGGER = LogUtil.getLogger(this.getClass());
 
 public SimpleIocApplication(Class<?> applicationEntryClass) {
 this.applicationEntryClass = applicationEntryClass;
 }
 
 public static void run(Class<?> applicationEntryClass, String[] args) {
 new SimpleIocApplication(applicationEntryClass).run(args);
 }
 
 public void run(String[] args) {
 LOGGER.info("start running......");
 
 // create application context and application initializer
 applicationContext = createSimpleApplicationContext();
 ApplicationContextInitializer initializer = createSimpleApplicationContextInitializer(applicationEntryClass);
 
 // initialize the application context (this is where we create beans)
 initializer.initialize(applicationContext); // here maybe exist a hidden cast
 
 // process those special beans
 processSpecialBeans(args);
 
 LOGGER.info("over!");
 }
 
 private SimpleApplicationContextInitializer createSimpleApplicationContextInitializer(Class<?> entryClass) {
 // get base packages
 SimpleIocBootApplication annotation = entryClass.getDeclaredAnnotation(SimpleIocBootApplication.class);
 String[] basePackages = annotation.basePackages();
 if (basePackages.length == 0) {
  basePackages = new String[]{entryClass.getPackage().getName()};
 }
 
 // create context initializer with base packages
 return new SimpleApplicationContextInitializer(Arrays.asList(basePackages));
 }
 
 private SimpleApplicationContext createSimpleApplicationContext() {
 return new SimpleApplicationContext();
 }
 
 private void processSpecialBeans(String[] args) {
 callRegisteredRunners(args);
 }
 
 private void callRegisteredRunners(String[] args) {
 Map<String, SimpleIocApplicationRunner> applicationRunners = applicationContext.getBeansOfType(SimpleIocApplicationRunner.class);
 try {
  for (SimpleIocApplicationRunner applicationRunner : applicationRunners.values()) {
  applicationRunner.run(args);
  }
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
 }
}

说明

前面说到应用的启动会委托SimpleIocApplication来完成,通过将应用入口类(测试程序中的SampleApplication)传入SimpleIocApplication的构造函数,构造出SimpleIocApplication的一个实例并运行run方法。在run方法中,会首先生成一个applicationContext,并调用SimpleApplicationContextInitializer来完成applicationContext的初始化(bean的扫描、装配)。然后调用processSpecialBeans来处理一些特殊的bean,如实现了SimpleIocApplicationRunner接口的bean会调用run方法来完成一些应用程序的启动任务。
这就是这个ioc容器的整个流程。

SimpleApplicationContextInitializer

代码

?
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
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
public class SimpleApplicationContextInitializer implements ApplicationContextInitializer<SimpleApplicationContext> {
 
  private Set<String> basePackages = new LinkedHashSet<>();
 
  public SimpleApplicationContextInitializer(List<String> basePackages) {
    this.basePackages.addAll(basePackages);
  }
 
  @Override
  public void initialize(SimpleApplicationContext applicationContext) {
    try {
      applicationContext.scan(basePackages, true);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    applicationContext.setStartupDate(System.currentTimeMillis());
  }
}

说明

在SimpleIocApplication的run中,会根据basePackages来构造一个SimpleApplicationContextInitializer 的实例,进而通过这个ApplicationContextInitializer来完成SimpleApplicationContext 的初始化。
在SimpleApplicationContextInitializer中, 简单地调用SimpleApplicationContext 中的scan即可完成SimpleApplicationContext的初始化任务

SimpleApplicationContext

说明:

终于到了最重要的部分了,在SimpleApplicationContext中将真正完成扫描、生成bean以及自动装配的任务。这里scan即为SimpleApplicationContext的程序入口,由SimpleApplicationContextInitializer在初始化时调用。
代码的调用逻辑简单易懂,就不多加说明了。
这里只简单列一下各个字段的含义以及几个比较关键的方法的作用。

字段

- startupDate:启动时间记录字段
- scannedPackages:已经扫描的包的集合,保证不重复扫描
- registeredBeans:已经完全装配好并注册好了的bean
- earlyBeans : 只是生成好了,还未装配完成的bean,用于处理循环依赖的问题
- totalBeanCount : 所有bean的计数器,在生成bean的名字时会用到其唯一性

方法

- processEarlyBeans:用于最终装配earlyBeans 中的bean,若装配成功,则将bean移至registeredBeans,否则报错
- scan : 扫描并处理传入的package集合
- processSingleClass:处理单个类,尝试生成该类的bean并进行装配(前提是此类有@SimpleComponent注解)
- createBeansByMethodsOfClass : 顾名思义,根据那些被@Bean注解的方法来生成bean
- autowireFields:尝试装配某个bean,lastChance代表是否在装配失败是报错(在第一次装配时,此值为false,在装配失败后会将bean移至earlyBeans,在第二次装配时,此值为true,实际上就是在装配earlyBeans中的bean,因此若仍然装配失败,就会报错)。在这个方法中,装配相应的bean时会从registeredBeans以及earlyBeans中去寻找符合条件的bean,只要找到,不管是来自哪里,都算装配成功。

代码

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import com.clayoverwind.simpleioc.context.annotation.SimpleAutowired;
import com.clayoverwind.simpleioc.context.annotation.SimpleBean;
import com.clayoverwind.simpleioc.context.annotation.SimpleComponent;
import com.clayoverwind.simpleioc.context.factory.Bean;
import com.clayoverwind.simpleioc.util.ClassUtil;
import com.clayoverwind.simpleioc.util.ConcurrentHashSet;
import com.clayoverwind.simpleioc.util.LogUtil;
 
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
 
/**
 * @author clayoverwind
 * @E-mail clayanddev@163.com
 * @version 2017/4/5
 */
 
public class SimpleApplicationContext implements ApplicationContext {
 
 private long startupDate;
 
 private Set<String> scannedPackages = new ConcurrentHashSet<>();
 
 private Map<String, Bean> registeredBeans = new ConcurrentHashMap<>();
 
 private Map<String, Bean> earlyBeans = new ConcurrentHashMap<>();
 
 private final Logger LOGGER = LogUtil.getLogger(this.getClass());
 
 AtomicLong totalBeanCount = new AtomicLong(0L);
 
 AtomicLong nameConflictCount = new AtomicLong(0L);
 
 @Override
 public Object getBean(String name) {
 return registeredBeans.get(name);
 }
 
 @Override
 public <T> T getBean(String name, Class<T> type) {
 Bean bean = (Bean)getBean(name);
 return bean == null ? null : (type.isAssignableFrom(bean.getClazz()) ? type.cast(bean.getObject()) : null);
 }
 
 @Override
 public <T> T getBean(Class<T> type) {
 Map<String, T> map = getBeansOfType(type);
 return map.isEmpty() ? null : type.cast(map.values().toArray()[0]);
 }
 
 @Override
 public boolean containsBean(String name) {
 return getBean(name) != null;
 }
 
 @Override
 public <T> Map<String, T> getBeansOfType(Class<T> type) {
 Map<String, T> res = new HashMap<>();
 registeredBeans.entrySet().stream().filter(entry -> type.isAssignableFrom(entry.getValue().getClazz())).forEach(entry -> res.put(entry.getKey(), type.cast(entry.getValue().getObject())));
 return res;
 }
 
 @Override
 public void setStartupDate(long startupDate) {
 this.startupDate = startupDate;
 }
 
 @Override
 public long getStartupDate() {
 return startupDate;
 }
 
 /**
 * try to autowire those beans in earlyBeans
 * if succeed, remove it from earlyBeans and put it into registeredBeans
 * otherwise ,throw a RuntimeException(in autowireFields)
 */
 private synchronized void processEarlyBeans() {
 for (Map.Entry<String, Bean> entry : earlyBeans.entrySet()) {
  Bean myBean = entry.getValue();
  try {
  if (autowireFields(myBean.getObject(), myBean.getClazz(), true)) {
   registeredBeans.put(entry.getKey(), myBean);
   earlyBeans.remove(entry.getKey());
  }
  } catch (IllegalAccessException e) {
  throw new RuntimeException(e);
  }
 }
 }
 
 /**
 * scan base packages and create beans
 * @param basePackages
 * @param recursively
 * @throws ClassNotFoundException
 */
 public void scan(Set<String> basePackages, boolean recursively) throws ClassNotFoundException, IOException {
 LOGGER.info("start scanning......");
 
 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 
 // get all classes who haven't been registered
 Set<Class<?>> classes = new LinkedHashSet<>();
 for (String packageName : basePackages) {
  if (scannedPackages.add(packageName)) {
  classes.addAll(ClassUtil.getClassesByPackageName(classLoader, packageName, recursively));
  }
 }
 
 // autowire or create bean for each class
 classes.forEach(this::processSingleClass);
 
 processEarlyBeans();
 
 LOGGER.info("scan over!");
 }
 
 /**
 * try to create a bean for certain class, put it into registeredBeans if success, otherwise put it into earlyBeans
 * @param clazz
 */
 private void processSingleClass(Class<?> clazz) {
 LOGGER.info(String.format("processSingleClass [%s] ...", clazz.getName()));
 
 Annotation[] annotations = clazz.getDeclaredAnnotations();
 for (Annotation annotation : annotations) {
  if (annotation instanceof SimpleComponent) {
  Object instance;
  try {
   instance = clazz.newInstance();
  } catch (InstantiationException e) {
   throw new RuntimeException(e);
  } catch (IllegalAccessException e) {
   throw new RuntimeException(e);
  }
 
  long beanId = totalBeanCount.getAndIncrement();
  SimpleComponent component = (SimpleComponent) annotation;
  String beanName = component.value();
  if (beanName.isEmpty()) {
   beanName = getUniqueBeanNameByClassAndBeanId(clazz, beanId);
  }
 
  try {
   if (autowireFields(instance, clazz, false)) {
   registeredBeans.put(beanName, new Bean(instance, clazz));
   } else {
   earlyBeans.put(beanName, new Bean(instance, clazz));
   }
  } catch (IllegalAccessException e) {
   throw new RuntimeException(e);
  }
 
  try {
   createBeansByMethodsOfClass(instance, clazz);
  } catch (InvocationTargetException e) {
   throw new RuntimeException(e);
  } catch (IllegalAccessException e) {
   throw new RuntimeException(e);
  }
  }
 }
 }
 
 private void createBeansByMethodsOfClass(Object instance, Class<?> clazz) throws InvocationTargetException, IllegalAccessException {
 List<Method> methods = getMethodsWithAnnotation(clazz, SimpleBean.class);
 for (Method method : methods) {
  method.setAccessible(true);
  Object methodBean = method.invoke(instance);
  long beanId = totalBeanCount.getAndIncrement();
  Class<?> methodBeanClass = methodBean.getClass();
 
  //bean name
  SimpleBean simpleBean = method.getAnnotation(SimpleBean.class);
  String beanName = simpleBean.value();
  if (beanName.isEmpty()) {
  beanName = getUniqueBeanNameByClassAndBeanId(clazz, beanId);
  }
 
  // register bean
  registeredBeans.put(beanName, new Bean(methodBean, methodBeanClass));
 }
 }
 
 private List<Method> getMethodsWithAnnotation(Class<?> clazz, Class<?> annotationClass) {
 List<Method> res = new LinkedList<>();
 Method[] methods = clazz.getDeclaredMethods();
 for (Method method : methods) {
  Annotation[] annotations = method.getAnnotations();
  for (Annotation annotation : annotations) {
  if (annotation.annotationType() == annotationClass) {
   res.add(method);
   break;
  }
  }
 }
 return res;
 }
 
 
 /**
 * try autowire all fields of a certain instance
 * @param instance
 * @param clazz
 * @param lastChance
 * @return true if success, otherwise return false or throw a exception if this is the lastChance
 * @throws IllegalAccessException
 */
 private boolean autowireFields(Object instance, Class<?> clazz, boolean lastChance) throws IllegalAccessException {
 Field[] fields = clazz.getDeclaredFields();
 for (Field field : fields) {
  Annotation[] annotations = field.getAnnotations();
  for (Annotation annotation : annotations) {
  if (annotation instanceof SimpleAutowired) {
   SimpleAutowired autowired = (SimpleAutowired) annotation;
   String beanName = autowired.value();
   Bean bean = getSimpleBeanByNameOrType(beanName, field.getType(), true);
   if (bean == null) {
   if (lastChance) {
    if (!autowired.required()) {
    break;
    }
    throw new RuntimeException(String.format("Failed in autowireFields : [%s].[%s]", clazz.getName(), field.getName()));
   } else {
    return false;
   }
   }
   field.setAccessible(true);
   field.set(instance, bean.getObject());
  }
  }
 }
 return true;
 }
 
 /**
 * only used in autowireFields
 * @param beanName
 * @param type
 * @param allowEarlyBean
 * @return
 */
 private Bean getSimpleBeanByNameOrType(String beanName, Class<?> type, boolean allowEarlyBean) {
 // 1. by name
 Bean res = registeredBeans.get(beanName);
 if (res == null && allowEarlyBean) {
  res = earlyBeans.get(beanName);
 }
 
 // 2. by type
 if (type != null) {
  if (res == null) {
  res = getSimpleBeanByType(type, registeredBeans);
  }
  if (res == null && allowEarlyBean) {
  res = getSimpleBeanByType(type, earlyBeans);
  }
 }
 
 return res;
 }
 
 /**
 * search bean by type in certain beans map
 * @param type
 * @param beansMap
 * @return
 */
 private Bean getSimpleBeanByType(Class<?> type, Map<String, Bean> beansMap) {
 List<Bean> beans = new LinkedList<>();
 beansMap.entrySet().stream().filter(entry -> type.isAssignableFrom(entry.getValue().getClazz())).forEach(entry -> beans.add(entry.getValue()));
 if (beans.size() > 1) {
  throw new RuntimeException(String.format("Autowire by type, but more than one instance of type [%s] is founded!", beans.get(0).getClazz().getName()));
 }
 return beans.isEmpty() ? null : beans.get(0);
 }
 
 private String getUniqueBeanNameByClassAndBeanId(Class<?> clazz, long beanId) {
 String beanName = clazz.getName() + "_" + beanId;
 while (registeredBeans.containsKey(beanName) || earlyBeans.containsKey(beanName)) {
  beanName = clazz.getName() + "_" + beanId + "_" + nameConflictCount.getAndIncrement();
 }
 return beanName;
 }
}

后记

至此,一个简单的ioc容器就完成了,总结一下优缺点。

优点:

小而简单。
可以使用@SimpleBean、@SimpleComponent以及@SimpleAutowired 来完成一些简单但常用的依赖注入任务.

缺点:

很明显,实现过于简单,提供的功能太少。
如果你想了解ioc的实现原理,或者你想要开发一个小型个人项目但又嫌spring过于庞大,这个简单的ioc容器或许可以帮到你。

如果你想做的不仅如此,那么你应该将目光转向spring-boot

完整代码参考:https://github.com/clayandgithub/simple-ioc

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐