侧边栏壁纸
  • 累计撰写 101 篇文章
  • 累计创建 89 个标签
  • 累计收到 9 条评论

spring系列笔记 - 第二章 第一个 Spring 程序

bearjun
2020-11-18 / 0 评论 / 0 点赞 / 1,420 阅读 / 3,979 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2020-11-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1.软件版本

2.环境搭建

  • ***设置 pom ***
<!--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
  3. ⽇后应⽤Spring框架时,需要进⾏配置⽂件路径的设置。

70225-6uh6xk0d8xx.png

3.Spring的核⼼API

3.1 ApplicationContext
作⽤:Spring提供的ApplicationContext这个⼯⼚,⽤于对象的创建
好处:解耦合
3.2 ApplicationContext接⼝类型
接⼝:屏蔽实现的差异
⾮web环境(main函数、junit测试中):ClassPathXmlApplicationContext
web环境:XmlWebApplicationContext

31687-iboeqiu3yr.png

3.3 ApplicationContext重量级资源
因为:ApplicationContext⼯⼚的对象(两个实现类)占⽤⼤量内存。
所以:不会频繁的创建对象 : ⼀个应⽤只会创建⼀个⼯⼚对象。
所以:ApplicationContext⼯⼚:⼀定是线程安全的(多线程并发访问)

4.程序开发

前面一节已经讲了,现在来回顾下:

  • 创建类型
public class Person {}
  • 配置⽂件的配置
    applicationContext.xml中配置如下:
<?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">
    <!-- 
    id属性就是取名字
    class属性配置全路径名
    -->
    <bean id="person" class="com.bearjun.basic.Person"/>
</beans>
  • 通过⼯⼚类,获得对象
    ApplicationContext的实现类ClassPathXmlApplicationContext来获取配置文件
/**
 * 用于测试Spring的第一个程序
 */
@Test
public void test() {
    // 1、获取spring的工厂
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    // 2、通过工厂类获得对象
    Person person = (Person)ctx.getBean("person");
    System.out.println(person);
}

5.细节分析

5.1 名词解释

名词解释:Spring⼯⼚创建的对象,叫做bean或者组件(componet);
所以,以后不管叫bean,或者是叫组件,都是由spring工厂创建的对象。

5.2 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.3 配置⽂件中需要注意的细节

1、如果bean只配置class属性?会生成id嘛?

<bean class="com.bearjun.basic.Person"/>

会自动生成一个id
全路径命名#数字,例如com.bearjun.basic.Person#1
可以使用 getBeanNamesForType 验证。
应⽤场景:
如果这个bean只需要使⽤⼀次,那么就可以省略id值;
如果这个bean会使⽤多次,或者被其他bean引⽤则需要设置id值;

2、name属性:

作⽤:⽤于在Spring的配置⽂件中,为bean对象定义别名(取小名)

<bean id="person" name="p" class="com.bearjun.basic.Person"/>
Person person = (Person)ctx.getBean("p");
System.out.println("person = " + person);

相同点:

  • ctx.getBean("id")ctx.getBean("name") 都可以创建bean对象;
  • <bean id="person" class="Person"/><bean name="person" class="Person"/> 等效;

区别:

  • 别名可以定义多个(name后面跟多个别名,用‘,’隔开),但是 id 属性只能有⼀个值;
  • XML(spring的配置文件)的id属性的值,命名要求:必须以字⺟开头,后面跟上字⺟、数字、下划线、连字符;不能以特殊字符开头 /person开头不允许;
    XML的name属性的值,命名没有要求,/person 可以。name属性会应用到特殊的命名场景下。
    但其实XML发展到了今天:ID属性的限制已经不存在,/person也可以。
  • getBeangetBeanDefinitionNames获取bean的区别。

6.Spring⼯⼚的底层实现原理(简易版)

Spring⼯⼚是可以调⽤对象私有的构造⽅法创建对象

20200521002624493.png

  • classPathXmlApplicationContext工厂读去配置文件application.xml
  • 通过id获取类的权限类名
  • 反射获取类的对象

7.思考?

问题:在未来的开发过程中,是不是所有的对象都交给Spring工厂来创建那

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

0

评论区