第一个Spring程序【Spring学习笔记二】

427 阅读2分钟

1、软件版本

1. JDK1.8+
2. Maven3.5+
3. IDEA2018+
4. SpringFramework 5.1.4
	官网 www.spring.io

2、环境搭建

  • Spring的jar包

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.4.RELEASE</version>
            </dependency>
    
  • Spring的配置文件

    1. 配置文件的放置位置:任意位置 没有硬性要求
    2. 配置文件的命名:没有硬性要求 建议:applicationContext.xml
    
    思考:日后应用Spring框架时,需要进行配置文件路径的设置
    

QQ截图20211029233318.png

3、Spring的核心API

  • ApplicationContext

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

      接口:屏蔽实现的差异
      非web环境:ClassPathXmlApplicationContext(main junit)
      web环境:XmlWebApplicationContext
      

QQ截图20211029233707.png

  • 重量级资源

    ApplicationContext工厂的对象占用大量内存
    不会频繁的创建对象:一个应用只会创建一个工厂对象
    ApplicationContext工厂:一定时线程安全的(多线程并发访问)
    

4、程序开发

1. 创建类型
2. 配置文件的配置 applicationContext.xml
	<bean id="person" class="cn.edu.njtech.Person"></bean>
3. 通过工厂类,获得对象
	ApplicationContext
		|- ClassPathXmlApplicationContext
    // 1.获得Spring的工厂
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
    // 2.通过工厂类 获得对象
    Person person = (Person) applicationContext.getBean("person");
    System.out.println("person = " + person);

5、细节分析

  • 名词解释

    Spring工厂创建的对象,叫做bean或者组件(componet)
    
  • Spring工厂的相关的方法

    @Test
    public void test4() {
        // 1.获得Spring的工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
    
        /*
        省去强制转化
        */
        Person person = applicationContext.getBean("person", Person.class);
    
    
        /*
        有前提:需要只有一个bean标签的class是Person类型
        No qualifying bean of type 'cn.edu.njtech.Person' available: expected single matching bean but found 2: person,personAnother
        */
        Person person1 = applicationContext.getBean(Person.class);
    
    
        /*
        获取的是Spring工厂配置文件中所有bean标签的id值 person personAnother
        */
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String str : beanDefinitionNames) {
        System.out.println(str);
        }
    
        /*
        根据类型获得Spring配置文件中对应的id值
        */
        String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String str : beanNamesForType) {
        System.out.println(str);
        }
    
    
        /*
        用于判断是否存在指定id值的bean
        */
        boolean b1 = applicationContext.containsBeanDefinition("person");
    
        /*
        用于判断是否存在指定id值的bean
        */
        boolean b2 = applicationContext.containsBean("person");
        System.out.println(b2);
    }
    
  • 配置文件中需要注意的细节

    1. 只配置class属性
        <bean class="cn.edu.njtech.Person"></bean>
    a) 上述这种配置 有id值 cn.edu.njtech.basic.Persion#0
    b) 应用场景:如果这个bean只需要使用一次,那么就可以省略id值
    		   如果这个bean会使用多次,或者被其他bean引用则需要设置id值
    2. name属性
    作用:用于在Spring的配置文件中,为bean对象定义别名
    相同:
    	1. ctx.getBean("id|name") --> object
    	2. <bean id = "" class = ""
    		等效
    	   <bean name = "" class = ""
    区别:
    	1. 别名可以定义多个,但是id属性值只能有一个值
    	2. XML的id属性的值,命令要求:必须以字母开头,字母 数组 下划线 连字符 不能以特殊字符开头 /person(不可以)
    		name属性的值 命名没有要求 /person(可以)
    		name属性会应用在特殊命名的场景下,/person(spring+structs1)
    		
    		XML发展到了今天:ID属性的限制不存在 /person(可以)
        3. 代码
        	ctx.containsBeanDefinition("person") // 能识别id,不能识别name
        	ctx.containsBean("person") // 能识别id和name
    

6、Spring工程底层实现原理(简易版)

Spring工厂是可以调用对象私有的构造方法创建对象

微信截图_20211101085358.png

7、思考

1. 问题:未来在开发过程中,是不是所有的对象,都会交给Spring工厂来创建呢?
2. 回答:理论上 是的,但是有特例:实体对象(entity)是不会交给Spring创建,它是由持久层框架进行创建

8、Spring5.x与日志框架的整合

1. Spring与日志框架进行整合,日志框架就可以在控制台中,输出Spring框架运行过程中的一些重要的信息。
2. 好处:便于了解Spring的框架的运行过程,利于程序的调试。
  • Spring如何整合日志框架

    默认:
    	Spring1.2.3早期都是与commons-logging.jar
    	Spring5.x默认整合的日志框架 logback log4j2
    Spring5.x整合log4j
    	1. 引入log4j jar包
    	2. 引入log4.properties配置文件
    
    • pom

      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.12</version>
      </dependency>
      
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.25</version>
      </dependency>
      
    • log4j.properties

      # resources文件夹根目录下
      ### 配置根
      log4j.rootLogger=debug,console
      ### 日志输出到控制台显示
      log4j.appender.console=org.apache.log4j.ConsoleAppender
      log4j.appender.console.Target=System.out
      log4j.appender.console.layout=org.apache.log4j.PatternLayout
      log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n