ToB企服应用市场:ToB评测及商务社交产业平台

标题: Spring源码系列一:入门——Hello World [打印本页]

作者: 锦通    时间: 2023-4-7 23:29
标题: Spring源码系列一:入门——Hello World
前言

讲解Spring之前,我们首先梳理下Spring有哪些知识点可以进行入手源码分析,比如:
Hello World

通过这些知识点,后续我们慢慢在深入Spring的使用及原理剖析,为了更好地理解Spring,我们需要先了解一个最简单的示例——Hello World。在学习任何框架和语言之前,Hello World都是必不可少的。
  1. //在以前大家都是spring.xml进行注入bean后供Spring框架解析
  2. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  3. UserService userService = (UserService) context.getBean("userService");
  4. userService.test();
复制代码
spring.xml中的内容为:
  1. [/code]如果对上面的代码或者xml形式很陌生,再看下面一种代码,也是目前流行的一种形式
  2. [code]//通过我们的配置类进行注入bean并解析
  3. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);
  4. //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  5. UserService userService = (UserService) context.getBean("userService");
  6. userService.test()
复制代码
MySpringConfig中的内容为:
  1. @ComponentScan("com.xiaoyu")
  2. public class MySpringConfig {
  3.         @Bean
  4.         public UserService userService(){
  5.                 return new UserService();
  6.         }
  7. }
复制代码
相信很多人都会对上面的代码不陌生,那我们来看下这三行代码都做了那些工作:
虽然我们目前很少直接使用这种方式来使用Spring,而是使用Spring MVC或者Spring Boot,但是它们都是基于上面这种方式的,都需要在内部去创建一个
ApplicationContext的,只不过:
根据上面这三步我们大概找到了进行源码深入的方向,本章只是简单讲解下如何简单使用Spring,先对这些流程有个印象,并带着疑惑来进一步探究Spring的内部机制,好了,今天就讲到这里,我是小雨,我们下期再见。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4