Spring学习笔记(二):第一个Spring程序

    技术2022-07-11  113

    Spring学习笔记:第一个Spring程序

    1.环境搭建2.Spring 的核心API3.程序开发4.Spring工厂的相关方法5.配置文件的细节1.配置class属性(在没配置id的情况下,Spring会默认给id值)应用场景 2.name属性 Spring工厂的底层实现原理(简易版)Spring5.x 与 日志框架 的整合Spring5.x 整合 log4j:

    1.环境搭建

    依赖查询网站:maven仓库;

    Spring 的配置文件: 配置⽂件的放置位置:任意位置,没有硬性要求;配置⽂件的命名 :没有硬性要求,建议:applicationContext.xml;

    思考:⽇后应⽤ Spring 框架时,需要进⾏配置⽂件路径的设置。

    2.Spring 的核心API

    ApplicationContext 作用:Spring 提供的 ApplicationContext 这个⼯⼚,⽤于对象的创建;好处:解耦合 ApplicationContext 是接⼝类型; 接⼝的作用:屏蔽实现的差异非web环境(指在单元测试junit或者main函数):ClassPathXmlApplicationContextweb环境 :XmlWebApplicationContext ApplicationContext 是一个重量级资源 ApplicationContext 工厂的对象占用大量内存不会频繁的创建对象:一个应用只会创建一个工厂对象ApplicationContext 工厂是线程安全(多线程并发访问)

    3.程序开发

    创建类型:Person.java public class Person {} 配置文件的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.yusael.basic.Person"/> </beans> 通过⼯⼚类,获得对象 /** * 用于测试Spring的第一个程序 */ @Test public void test() { // 1、获取spring的工厂 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); // 2、通过工厂类获得对象 Person person = (Person)ctx.getBean("person"); System.out.println(person); }

    名词解释:Spring 工厂创建的对象,叫做 bean 或者 组件(componet);

    4.Spring工厂的相关方法

    getBean:传入 id值 和 类名 获取对象,不需要强制类型转换。 // 通过这种⽅式获得对象,就不需要强制类型转换 Person person = ctx.getBean("person", Person.class); System.out.println("person = " + person); getBean:只指定类名,Spring 的配置文件中只能有一个 bean 是这个类型。 // 使用这种方式的话, 当前Spring的配置文件中 只能有一个bean class是Person类型 Person person = ctx.getBean(Person.class); System.out.println("person = " + person); getBeanDefinitionNames:获取 Spring 配置文件中所有的 bean 标签的 id 值。 // 获取的是Spring工厂配置文件中所有bean标签的id值 person person1 String[] beanDefinitionNames = ctx.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { System.out.println("beanDefinitionName = " + beanDefinitionName); } getBeanNamesForType:根据类型获得 Spring 配置文件中对应的 id 值。 // 根据类型获得Spring配置文件中对应的id值 String[] beanNamesForType = ctx.getBeanNamesForType(Person.class); for (String id : beanNamesForType) { System.out.println("id = " + id); } containsBeanDefinition:用于判断是否存在指定 id 值的 bean,不能判断 name 值。 // 用于判断是否存在指定id值的bean,不能判断name值 if (ctx.containsBeanDefinition("person")) { System.out.println(true); } else { System.out.println(false); } containsBean:用于判断是否存在指定 id 值的 bean,也可以判断 name 值。 // 用于判断是否存在指定id值的bean,也可以判断name值 if (ctx.containsBean("p")) { System.out.println(true); } else { System.out.println(false); }

    5.配置文件的细节

    1.配置class属性(在没配置id的情况下,Spring会默认给id值)

    <bean class="com.baizhiedu.basic.Person">

    应用场景

    如果这个Bean只需要使用一次,那么就可以省略id值如果这个Bean会使用多次,或者被其他bean引用则需要设置id值

    2.name属性

    作用:用于在Spring的配置文件中,为bean对象定义别名(小名)相同: ctx.getBean("id") 或 ctx.getBean("name") 都可以创建对象;<bean id="person" class="Person"/> 与 <bean name="person" class="Person"/> 等效; 区别: 别名可以定义多个,但是 id 属性只能有⼀个值;XML 的 id 属性的值,命名要求:必须以字⺟开头,可以包含 字⺟、数字、下划线、连字符;不能以特殊字符开头 比如:/person;XML 的 name 属性的值,命名没有要求,/person 可以。但其实 XML 发展到了今天:ID属性的限制已经不存在,/person也可以。

    Spring工厂的底层实现原理(简易版)

    问题:未来在开发过程中,是不是所有的对象,都会交给 Spring ⼯⼚来创建呢?

    回答:理论上是的,但是有特例 :实体对象(entity) 是不会交给Spring创建,它由持久层框架进⾏创建。

    Spring5.x 与 日志框架 的整合

    Spring 与⽇志框架进⾏整合,⽇志框架就可以在控制台中,输出Spring框架运⾏过程中的⼀ 些重要的信息。 好处:便于了解Spring框架的运⾏过程,利于程序的调试。

    Spring5.x 整合 log4j:

    引⼊ log4j.jar 包; <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> ⼊ log4.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
    Processed: 0.009, SQL: 9