ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

spring02—第一个spring程序

2021-09-10 16:35:06  阅读:194  来源: 互联网

标签:配置文件 Person spring 程序 spring02 person bean Spring id


环境搭建

依赖查询网站:https://mvnrepository.com/

配置 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 的配置文件:

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

Spring 的核心API


ApplicationContext

作用:Spring 提供的 ApplicationContext 这个工厂,用于对象的创建;
好处:解耦合
ApplicationContext 是接口类型;
接口:屏蔽实现的差异
非 web 环境 (main junit) :ClassPathXmlApplicationContext


web 环境 :XmlWebApplicationContext

在这里插入图片描述

  • 重量级资源:
    ApplicationContext 工厂的对象占用大量内存。
    不会频繁的创建对象 ,⼀个应用只会创建⼀个工厂对象。
    ApplicationContext 工厂:⼀定是线程安全的(多线程并发访问)。

程序开发

  1. 创建类型:Person.java
public class Person {}
  1. 配置文件的配置:
<?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>
  1. 通过工厂类,获得对象
/**
 * 用于测试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);

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);
}

配置文件中的细节

如果 bean 只配置 class 属性:

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

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

name 属性:

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

在这里插入图片描述

思考

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

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

 

标签:配置文件,Person,spring,程序,spring02,person,bean,Spring,id
来源: https://www.cnblogs.com/naybdo/p/15251807.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有