您的当前位置:首页正文

手写Spring框架 IOC 和 AOP

2024-11-25 来源:个人技术集锦

代码演示视频:链接:

该项目是博主帮某985大学学生做的一个项目

详细介绍:

一、功能介绍

实现了 IOC 和 AOP 功能,xml 配置和 注解扫描两种

xml 标签如 

beans

bean id class scope(single/prototype) autowired(byType/byName) name

property value ref name

constructor-arg 子标签 List Map Set ref value index type value ref

context:component-scan

等等

注解如

@Component @Service @Repository @Autowired @Qulififier @Value

二、代码结构

IOC

AOP

 

 

三、核心代码

1、BeanFactory



2、ApplicationContext


  1. package com.liuyanzhao.springframework.context;
  2. import com.liuyanzhao.springframework.beans.exception.BeansException;
  3. import com.liuyanzhao.springframework.beans.factory.BeanFactory;
  4. /**
  5. * 应用上下文
  6. * @Author: liuyanzhao
  7. * @Date: 2022/4/4 11:11
  8. * @Version 1.0
  9. */
  10. public interface ApplicationContext {
  11. /**
  12. * 刷新容器
  13. * 即初始化操作
  14. *
  15. * @throws BeansException
  16. */
  17. void refresh() ;
  18. /**
  19. * 从Bean单例池中获取Bean对象
  20. *
  21. * @param name
  22. * @return
  23. * @throws BeansException
  24. */
  25. Object getBean(String name) throws BeansException;
  26. /**
  27. * 同上,只是能根据泛型获取Bean对象,无需强制类型转换
  28. * *
  29. *
  30. * @param name
  31. * @param requiredType
  32. * @param <T>
  33. * @return
  34. * @throws BeansException
  35. */
  36. <T> T getBean(String name, Class<T> requiredType) throws BeansException;
  37. /**
  38. * 同上
  39. *
  40. * @param requiredType
  41. * @param <T>
  42. * @return
  43. * @throws BeansException
  44. */
  45. <T> T getBean(Class<T> requiredType) throws BeansException;
  46. /**
  47. * 也是获取Bean对象,只不过传入了一些成员变量的参数
  48. *
  49. * @param name
  50. * @param args
  51. * @return
  52. * @throws BeansException
  53. */
  54. Object getBean(String name, Object... args) throws BeansException;
  55. /**
  56. * 获得工厂对象
  57. * @return
  58. */
  59. BeanFactory getBeanFactory();
  60. /**
  61. * 销毁工厂
  62. * @throws Exception
  63. */
  64. void close() ;
  65. }

3、ClassPathXmlApplicationContext


  1. package com.liuyanzhao.springframework.context;
  2. import com.liuyanzhao.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
  3. import com.liuyanzhao.springframework.beans.exception.BeansException;
  4. import com.liuyanzhao.springframework.beans.factory.BeanFactory;
  5. import com.liuyanzhao.springframework.beans.factory.PropertyPlaceholderConfigurer;
  6. import com.liuyanzhao.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
  7. import com.liuyanzhao.springframework.beans.factory.config.BeanDefinition;
  8. import com.liuyanzhao.springframework.beans.factory.support.BeanDefinitionReader;
  9. import com.liuyanzhao.springframework.beans.factory.support.DefaultBeanFactory;
  10. import com.liuyanzhao.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  11. import java.util.Map;
  12. /**
  13. * classpath下读取XML文件的上下文实现
  14. *
  15. * @Author: liuyanzhao
  16. * @Date: 2022/4/4 11:14
  17. * @Version 1.0
  18. */
  19. public class ClassPathXmlApplicationContext implements ApplicationContext {
  20. /**
  21. * 配置文件位置,暂时只考虑一个配置文件的情况
  22. */
  23. private String[] configLocation;
  24. /**
  25. * Bean工厂
  26. */
  27. private BeanFactory beanFactory;
  28. public ClassPathXmlApplicationContext(String configLocation) {
  29. this.configLocation = new String[]{configLocation};
  30. // 直接调用初始化
  31. this.refresh();
  32. }
  33. public ClassPathXmlApplicationContext(String[] configLocation) {
  34. this.configLocation = configLocation;
  35. // 直接调用初始化
  36. this.refresh();
  37. }
  38. @Override
  39. public void refresh() {
  40. try {
  41. // 1.创建BeanFactory 工厂
  42. beanFactory = new DefaultBeanFactory();
  43. // 2.加载Bean定义
  44. BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
  45. beanDefinitionReader.loadBeanDefinitions(this.configLocation);
  46. // 3. Bean工厂后置处理器,即对Bean工厂做一些后置的处理
  47. // 获取配置文件里的属性值,如获取jdbc.proerties里的jdbc.name的值
  48. PropertyPlaceholderConfigurer placeholderConfigurer = (PropertyPlaceholderConfigurer) beanFactory.getBean("propertyPlaceholderConfigurer");
  49. if (placeholderConfigurer != null) {
  50. placeholderConfigurer.postProcessBeanFactory(beanFactory);
  51. }
  52. // 4.注册Bean工厂
  53. beanFactory.registerBeanDefinition("autowiredAnnotationBeanPostProcessor", new BeanDefinition(AutowiredAnnotationBeanPostProcessor.class));
  54. beanFactory.registerBeanDefinition("defaultAdvisorAutoProxyCreator", new BeanDefinition(DefaultAdvisorAutoProxyCreator.class));
  55. beanFactory.addBeanPostProcessor(beanFactory.getBean(AutowiredAnnotationBeanPostProcessor.class));
  56. beanFactory.addBeanPostProcessor(beanFactory.getBean(DefaultAdvisorAutoProxyCreator.class));
  57. // 5.提前实例化单例Bean(可选)
  58. Map<String, BeanDefinition> beanDefinitionMap = beanFactory.getBeanDefinitionMap();
  59. // for (String beanName : beanDefinitionMap.keySet()) {
  60. // Object bean = beanFactory.getBean(beanName);
  61. // System.out.println(bean);
  62. // }
  63. // 6.创建成功
  64. System.out.println("My-Spring容器初始化完成");
  65. System.out.println("共扫描到了 " + beanDefinitionMap.size() + " 个Bean定义,如下(Bean名称 --> Class)");
  66. System.out.println("--------------------------------------------------------------------------------------------------------------------------------------");
  67. for (Map.Entry<String, BeanDefinition> entry : beanDefinitionMap.entrySet()) {
  68. System.out.println(entry.getKey() + " --> " + entry.getValue().getBeanClass().getName());
  69. }
  70. System.out.println("--------------------------------------------------------------------------------------------------------------------------------------\n");
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. // 发生异常时,关闭上下文
  74. this.close();
  75. System.exit(0);
  76. }
  77. }
  78. @Override
  79. public Object getBean(String name) throws BeansException {
  80. return beanFactory.getBean(name);
  81. }
  82. @Override
  83. public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
  84. return beanFactory.getBean(name, requiredType);
  85. }
  86. @Override
  87. public <T> T getBean(Class<T> requiredType) throws BeansException {
  88. return beanFactory.getBean(requiredType);
  89. }
  90. @Override
  91. public Object getBean(String name, Object... args) throws BeansException {
  92. return beanFactory.getBean(name, args);
  93. }
  94. @Override
  95. public BeanFactory getBeanFactory() {
  96. return beanFactory;
  97. }
  98. @Override
  99. public void close() {
  100. System.out.println("开始关闭上下文");
  101. beanFactory.close();
  102. System.out.println("关闭上下文结束");
  103. }
  104. }

4、DefaultBeanFactory


  1. package com.liuyanzhao.springframework.beans.factory.support;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.liuyanzhao.springframework.beans.exception.BeansException;
  4. import com.example.springframework.beans.factory.*;
  5. import com.liuyanzhao.springframework.beans.factory.config.BeanDefinition;
  6. import com.liuyanzhao.springframework.beans.factory.config.BeanPostProcessor;
  7. import com.liuyanzhao.springframework.beans.factory.config.BeanReference;
  8. import com.liuyanzhao.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
  9. import com.liuyanzhao.springframework.util.StringValueResolver;
  10. import com.liuyanzhao.springframework.beans.factory.*;
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.*;
  14. /**
  15. * bean工厂默认实现类
  16. * 继承单例Bean注册器类
  17. *
  18. * @Author: liuyanzhao
  19. * @Date: 2022/4/3 15:42
  20. * @Version 1.0
  21. */
  22. public class DefaultBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
  23. /**
  24. * 定义一个Bean定义池子,维护Bean名称和Bean定义的关系(即名称和类类型的关系)
  25. */
  26. public Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
  27. /**
  28. * @Value的属性值集合
  29. */
  30. public List<StringValueResolver> embeddedValueResolvers = new ArrayList<>();
  31. /**
  32. * BeanPostProcessors to apply in createBean
  33. */
  34. public List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
  35. @Override
  36. public Object getBean(String name) throws BeansException {
  37. return doGetBean(name, null);
  38. }
  39. @Override
  40. public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
  41. return (T) this.getBean(name);
  42. }
  43. @Override
  44. public Object getBean(String name, Object... args) throws BeansException {
  45. return doGetBean(name, args);
  46. }
  47. @Override
  48. public <T> T getBean(Class<T> requiredType) throws BeansException {
  49. List<String> beanNames = new ArrayList<>();
  50. for (Map.Entry<String, BeanDefinition> entry : beanDefinitionMap.entrySet()) {
  51. Class beanClass = entry.getValue().getBeanClass();
  52. // 判断是否为子类或者接口实现的类
  53. // 如 Dog 是 Animal 的子类,为true;CCServiceImpl是CCService的接口实现,也为true
  54. if (requiredType.isAssignableFrom(beanClass)) {
  55. beanNames.add(entry.getKey());
  56. }
  57. }
  58. if (1 == beanNames.size()) {
  59. return getBean(beanNames.get(0), requiredType);
  60. }
  61. return null;
  62. }
  63. /**
  64. * 获取Bean
  65. *
  66. * @param name
  67. * @param args
  68. * @param <T>
  69. * @return
  70. */
  71. private <T> T doGetBean(final String name, final Object[] args) {
  72. // 先从单例池里拿,拿不到再创建
  73. Object bean = getSingleton(name);
  74. if (bean != null) {
  75. return (T) bean;
  76. }
  77. BeanDefinition beanDefinition = getBeanDefinition(name);
  78. if (beanDefinition == null) {
  79. return null;
  80. }
  81. return (T) createBean(name, beanDefinition, args);
  82. }
  83. @Override
  84. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
  85. beanDefinitionMap.put(beanName, beanDefinition);
  86. }
  87. @Override
  88. public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
  89. return beanDefinitionMap.get(beanName);
  90. }
  91. @Override
  92. public boolean containsBeanDefinition(String beanName) {
  93. return beanDefinitionMap.containsKey(beanName);
  94. }
  95. @Override
  96. public void autowireBean(Object existingBean) {
  97. String className = existingBean.getClass().getName();
  98. BeanDefinition beanDefinition = new BeanDefinition(existingBean.getClass());
  99. beanDefinitionMap.put(className, beanDefinition);
  100. this.createBean(className, existingBean, beanDefinition, new Object[]{});
  101. }
  102. @Override
  103. public Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
  104. return this.createBean(beanName, null, beanDefinition, args);
  105. }
  106. @Override
  107. public Object createBean(String beanName, Object existingBean, BeanDefinition beanDefinition, Object[] args) throws BeansException {
  108. Object bean;
  109. try {
  110. // 判断是否返回代理 Bean 对象
  111. bean = resolveBeforeInstantiation(beanName, beanDefinition);
  112. if (null != bean) {
  113. return bean;
  114. }
  115. // 实例化 Bean,如果已经提供了对象,直接使用
  116. bean = existingBean != null ? existingBean : this.createBeanInstance(beanDefinition, args);
  117. // 处理循环依赖,将实例化后的Bean对象提前放入缓存中暴露出来
  118. if (BeanDefinition.SCORE_SINGLETON.equals(beanDefinition.getScope())) {
  119. Object finalBean = bean;
  120. addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, beanDefinition, finalBean));
  121. }
  122. // 实例化后判断
  123. boolean continueWithPropertyPopulation = applyBeanPostProcessorsAfterInstantiation(beanName, bean);
  124. if (!continueWithPropertyPopulation) {
  125. return bean;
  126. }
  127. // 在设置 Bean 属性之前,允许 BeanPostProcessor 修改属性值
  128. applyBeanPostProcessorsBeforeApplyingPropertyValues(beanName, bean, beanDefinition);
  129. // 给 Bean 填充属性
  130. applyPropertyValues(beanName, bean, beanDefinition);
  131. // 执行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置处理方法
  132. bean = initializeBean(beanName, bean, beanDefinition);
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. throw new BeansException("Bean属性设置失败", e);
  136. }
  137. // 如果要求作用域为单例,则放到单例池中
  138. if (BeanDefinition.SCORE_SINGLETON.equals(beanDefinition.getScope())) {
  139. addSingleton(beanName, bean);
  140. }
  141. return bean;
  142. }
  143. protected Object getEarlyBeanReference(String beanName, BeanDefinition beanDefinition, Object bean) {
  144. Object exposedObject = bean;
  145. for (BeanPostProcessor beanPostProcessor : getBeanPostProcessors()) {
  146. exposedObject = beanPostProcessor.getEarlyBeanReference(exposedObject, beanName);
  147. if (null == exposedObject) {
  148. return exposedObject;
  149. }
  150. }
  151. return exposedObject;
  152. }
  153. /**
  154. * 执行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置处理方法
  155. *
  156. * @param beanName
  157. * @param bean
  158. * @param beanDefinition
  159. * @return
  160. */
  161. private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) {
  162. // 给 BeanPostProcessor 设置 BeanFactory
  163. if (bean instanceof BeanPostProcessor) {
  164. ((BeanPostProcessor) bean).setBeanFactory(this);
  165. }
  166. // 2. 执行 BeanPostProcessor Before 处理
  167. Object wrappedBean = applyBeanPostProcessorsBeforeInitialization(bean, beanName);
  168. // 3. 执行 BeanPostProcessor After 处理
  169. wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  170. return wrappedBean;
  171. }
  172. /**
  173. * 在设置 Bean 属性之前,允许 BeanPostProcessor 修改属性值
  174. *
  175. * @param beanName
  176. * @param bean
  177. * @param beanDefinition
  178. */
  179. protected void applyBeanPostProcessorsBeforeApplyingPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
  180. for (BeanPostProcessor beanPostProcessor : getBeanPostProcessors()) {
  181. PropertyValues pvs = beanPostProcessor.postProcessPropertyValues(beanDefinition.getPropertyValues(), bean, beanName);
  182. if (null != pvs) {
  183. for (PropertyValue propertyValue : pvs.getPropertyValues()) {
  184. beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * // 判断是否返回代理 Bean 对象
  191. *
  192. * @param beanName
  193. * @param beanDefinition
  194. * @return
  195. */
  196. private Object resolveBeforeInstantiation(String beanName, BeanDefinition beanDefinition) {
  197. Object bean = applyBeanPostProcessorsBeforeInstantiation(beanDefinition.getBeanClass(), beanName);
  198. if (null != bean) {
  199. bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
  200. }
  201. return bean;
  202. }
  203. /**
  204. * 在 Bean 对象执行实例化方法之前,执行此方法
  205. *
  206. * @param beanClass
  207. * @param beanName
  208. * @return
  209. */
  210. private Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
  211. for (BeanPostProcessor beanPostProcessor : getBeanPostProcessors()) {
  212. Object result = beanPostProcessor.postProcessBeforeInstantiation(beanClass, beanName);
  213. if (null != result) {
  214. return result;
  215. }
  216. }
  217. return null;
  218. }
  219. /**
  220. * 在 Bean 对象执行实例化方法之后,执行此方法
  221. * Bean 实例化后对于返回 false 的对象,不在执行后续设置 Bean 对象属性的操作
  222. *
  223. * @param beanName
  224. * @param bean
  225. * @return
  226. */
  227. private boolean applyBeanPostProcessorsAfterInstantiation(String beanName, Object bean) {
  228. boolean continueWithPropertyPopulation = true;
  229. for (BeanPostProcessor beanPostProcessor : getBeanPostProcessors()) {
  230. if (beanPostProcessor instanceof AutowiredAnnotationBeanPostProcessor) {
  231. if (!beanPostProcessor.postProcessAfterInstantiation(bean, beanName)) {
  232. continueWithPropertyPopulation = false;
  233. break;
  234. }
  235. }
  236. }
  237. return continueWithPropertyPopulation;
  238. }
  239. /**
  240. * 在 Bean 对象执行初始化方法之前,执行此方法
  241. *
  242. * @param existingBean
  243. * @param beanName
  244. * @return
  245. * @throws BeansException
  246. */
  247. public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
  248. Object result = existingBean;
  249. for (BeanPostProcessor processor : getBeanPostProcessors()) {
  250. Object current = processor.postProcessBeforeInitialization(result, beanName);
  251. if (null == current) return result;
  252. result = current;
  253. }
  254. return result;
  255. }
  256. /**
  257. * 在 Bean 对象执行初始化方法之后,执行此方法
  258. *
  259. * @param existingBean
  260. * @param beanName
  261. * @return
  262. * @throws BeansException
  263. */
  264. private Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
  265. Object result = existingBean;
  266. for (BeanPostProcessor processor : getBeanPostProcessors()) {
  267. Object current = processor.postProcessAfterInitialization(result, beanName);
  268. if (null == current) {
  269. return result;
  270. }
  271. result = current;
  272. }
  273. return result;
  274. }
  275. @Override
  276. public String[] getBeanDefinitionNames() {
  277. return beanDefinitionMap.keySet().toArray(new String[0]);
  278. }
  279. @Override
  280. public Map<String, BeanDefinition> getBeanDefinitionMap() {
  281. return beanDefinitionMap;
  282. }
  283. @Override
  284. public void addEmbeddedValueResolver(StringValueResolver valueResolver) {
  285. this.embeddedValueResolvers.add(valueResolver);
  286. }
  287. @Override
  288. public String resolveEmbeddedValue(String value) {
  289. String result = value;
  290. for (StringValueResolver resolver : this.embeddedValueResolvers) {
  291. result = resolver.resolveStringValue(result);
  292. }
  293. return result;
  294. }
  295. @Override
  296. public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
  297. Map<String, T> result = new HashMap<>();
  298. beanDefinitionMap.forEach((beanName, beanDefinition) -> {
  299. Class beanClass = beanDefinition.getBeanClass();
  300. if (type.isAssignableFrom(beanClass)) {
  301. result.put(beanName, (T) getBean(beanName));
  302. }
  303. });
  304. return result;
  305. }
  306. @Override
  307. public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
  308. this.beanPostProcessors.remove(beanPostProcessor);
  309. this.beanPostProcessors.add(beanPostProcessor);
  310. }
  311. @Override
  312. public void close() {
  313. System.out.println("开始关闭Bean工厂");
  314. // 清空单例池
  315. Map<String, Object> singletonObjects = getSingletonObjects();
  316. for (Map.Entry<String, Object> entry : singletonObjects.entrySet()) {
  317. Object value = entry.getValue();
  318. String key = entry.getKey();
  319. value = null;
  320. singletonObjects.remove(key);
  321. }
  322. System.out.println("关闭Bean工厂结束");
  323. }
  324. /**
  325. * Bean 属性填充
  326. */
  327. protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
  328. try {
  329. PropertyValues propertyValues = beanDefinition.getPropertyValues();
  330. for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
  331. String name = propertyValue.getName();
  332. Object value = propertyValue.getValue();
  333. if (value instanceof BeanReference) {
  334. // A 依赖 B,获取 B 的实例化
  335. // 判断注入类型是 byType 还是 byName
  336. BeanReference beanReference = (BeanReference) value;
  337. if (BeanDefinition.AUTOWIRED_BY_NAME.equals(beanDefinition.getAutowired())) {
  338. // 根据 name 获取Bean
  339. value = getBean(beanReference.getBeanName());
  340. } else if (BeanDefinition.AUTOWIRED_BY_TYPE.equals(beanDefinition.getAutowired())) {
  341. // 根据 类类型即class 获取Bean
  342. value = getBean(beanReference.getBeanClass());
  343. } else {
  344. // 先根据 name 获取,获取不到再根据 class 获取
  345. value = getBean(beanReference.getBeanName(), beanReference.getBeanClass());
  346. }
  347. value = getBean(beanReference.getBeanName());
  348. }
  349. // 属性填充
  350. BeanUtil.setFieldValue(bean, name, value);
  351. }
  352. } catch (Exception e) {
  353. throw new BeansException("Error setting property values:" + beanName);
  354. }
  355. }
  356. /**
  357. * 创建Bean对象
  358. *
  359. * @param beanDefinition
  360. * @param args
  361. * @return
  362. */
  363. private Object createBeanInstance(BeanDefinition beanDefinition, Object[] args) {
  364. ConstructorArgValue[] constructorValues = {};
  365. ConstructorArgValues constructorArgValues = beanDefinition.getConstructorArgValues();
  366. if (constructorArgValues != null) {
  367. constructorValues = beanDefinition.getConstructorArgValues().getConstructorValues();
  368. }
  369. if (constructorValues != null && constructorValues.length > 0) {
  370. args = new Object[constructorValues.length];
  371. }
  372. Constructor constructorToUse = null;
  373. Class<?> beanClass = beanDefinition.getBeanClass();
  374. Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
  375. for (Constructor ctor : declaredConstructors) {
  376. if (null != args && ctor.getParameterTypes().length == args.length) {
  377. constructorToUse = ctor;
  378. // 处理下 args
  379. this.handlerConstructorArgValues(ctor, constructorArgValues, args);
  380. break;
  381. }
  382. }
  383. Class clazz = beanDefinition.getBeanClass();
  384. try {
  385. if (null != constructorToUse) {
  386. // 调用有参构造器,传参数
  387. return clazz.getDeclaredConstructor(constructorToUse.getParameterTypes()).newInstance(args);
  388. } else {
  389. // 调用无参构造器
  390. return clazz.getDeclaredConstructor().newInstance();
  391. }
  392. } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
  393. throw new BeansException("创建Bean失败 [" + clazz.getName() + "]", e);
  394. }
  395. }
  396. /**
  397. * 处理构造器参数
  398. *
  399. * @param ctor
  400. * @param constructorArgValues
  401. * @param args
  402. */
  403. private void handlerConstructorArgValues(Constructor ctor, ConstructorArgValues constructorArgValues, Object[] args) {
  404. ConstructorArgValue[] constructorValues = constructorArgValues.getConstructorValues();
  405. int i = 0;
  406. for (Class parameterType : ctor.getParameterTypes()) {
  407. // 先根据构造方法参数里的index找参数
  408. ConstructorArgValue constructorValue = constructorArgValues.getConstructorValueByIndex(String.valueOf(i));
  409. if (constructorValue == null) {
  410. // 找不到,直接当前这个参数
  411. constructorValue = constructorValues[i];
  412. }
  413. // 目前只支持String类型、常见基本类型
  414. if (parameterType == String.class) {
  415. args[i] = constructorValue.getValue();
  416. } else if (parameterType == Byte.class) {
  417. args[i] = Byte.valueOf(constructorValue.getValue());
  418. } else if (parameterType == Short.class) {
  419. args[i] = Short.valueOf(constructorValue.getValue());
  420. } else if (parameterType == Integer.class) {
  421. args[i] = Integer.valueOf(constructorValue.getValue());
  422. } else if (parameterType == Long.class) {
  423. args[i] = Long.valueOf(constructorValue.getValue());
  424. } else if (parameterType == Double.class) {
  425. args[i] = Double.valueOf(constructorValue.getValue());
  426. } else if (parameterType == Float.class) {
  427. args[i] = Float.valueOf(constructorValue.getValue());
  428. } else if (parameterType == Boolean.class) {
  429. args[i] = Boolean.valueOf(constructorValue.getValue());
  430. }
  431. i++;
  432. }
  433. }
  434. public List<StringValueResolver> getEmbeddedValueResolvers() {
  435. return embeddedValueResolvers;
  436. }
  437. public List<BeanPostProcessor> getBeanPostProcessors() {
  438. return beanPostProcessors;
  439. }
  440. }

等等

需要完整代码可以联系博主

显示全文