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

打印 上一主题 下一主题

主题 891|帖子 891|积分 2683

学习视频:【孙哥说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的依赖
    1.         <dependency>
    2.             <groupId>org.springframework</groupId>
    3.             <artifactId>spring-context</artifactId>
    4.             <version>5.1.4.RELEASE</version>
    5.         </dependency>
    复制代码
  • Spring的配置文件
    1.配置文件的放置位置:任意位置 没有硬性要求
    2.配置文件的命名:没有硬性要求 建议 applicationContext.xml
    思考:日后应用Spring框架时,需要进行配置文件路径的位置。

3.Spring的核心API


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

    • ApplicationContext接口类型
      接口:屏蔽实现的差异
      非web环境:ClassPathXmlApplicationContext(例如:main  junit)
      web环境:XmlWebApplicationContext

    可以看到ApplicationContext就是一个接口

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

4.程序开发

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

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

<ul>名此解释
Spring工厂创建的对象叫做bean
Spring工厂的相关方法
[code]Person person = context.getBean("person", Person.class);        System.out.println("person = " + person);        当前Spring的配置文件中只能有一个

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表