篮之新喜 发表于 2023-12-18 19:03:52

Spring5学习随笔-Spring5的第一个程序(环境搭建、日志框架整合)

学习视频:【孙哥说Spring5:从设计模式到基本应用到应用级底层分析,一次深入浅出的Spring全探索。学不会Spring?只因你未遇见孙哥】
第二章、第一个Spring程序

1.软件版本

1.JDK1.8+
2.Maven3.5+
3.IDEA2018+
4.SpringFramework 5.1.4
官网:www.spring.io
2.环境搭建


[*]Spring的jar包
1.设置pom的依赖
      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
      </dependency>
[*]Spring的配置文件
1.配置文件的放置位置:任意位置 没有硬性要求
2.配置文件的命名:没有硬性要求 建议 applicationContext.xml
思考:日后应用Spring框架时,需要进行配置文件路径的位置。
https://img2023.cnblogs.com/blog/3321544/202311/3321544-20231114103833238-1822025324.png
3.Spring的核心API


[*]ApplicationContext
作用:Spring提供的ApplicationContext这个工厂,用于对象的创建
好处:解耦合

[*]ApplicationContext接口类型
接口:屏蔽实现的差异
非web环境:ClassPathXmlApplicationContext(例如:mainjunit)
web环境:XmlWebApplicationContext
https://img2023.cnblogs.com/blog/3321544/202311/3321544-20231114103932588-599650590.png
可以看到ApplicationContext就是一个接口

[*]重量级资源(对象占用内存多就是重量级资源。)
1.ApplicationContext工厂的对象占用大量内存(指的是下面的实现类)
2.不会频繁的创建对象:一个应用程序只会创建一个工厂对象
3.ApplicationContext工厂:一定是线程安全的(多线程并发访问)

4.程序开发

spring开发的4个步骤
1.创建类型
2.配置文件的配置 applicationContext.xml

3.通过工厂类 获得对象
ApplicationContext | ClassPathXmlApplicationContext
//1.获取Spring的工厂
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
//2 通过工厂类获取对象
Person person = (Person) context.getBean("person");5.细节分析

<ul>名此解释
Spring工厂创建的对象叫做bean
Spring工厂的相关方法
Person person = context.getBean("person", Person.class);      System.out.println("person = " + person);      当前Spring的配置文件中只能有一个
页: [1]
查看完整版本: Spring5学习随笔-Spring5的第一个程序(环境搭建、日志框架整合)