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

这篇博客介绍了如何搭建Spring环境,重点讲解了Spring的核心APIApplicationContext及其在非web和web环境中的使用。此外,还详细阐述了配置文件的细节,包括class属性和name属性的应用,并探讨了Spring工厂的底层实现原理以及Spring5.x与log4j的整合步骤,旨在帮助初学者理解Spring框架的基础操作。
摘要由CSDN通过智能技术生成

1.环境搭建

依赖查询网站:maven仓库

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

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

2.Spring 的核心API

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

3.程序开发

  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)

4.Spring工厂的相关方法

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

在这里插入图片描述
在这里插入图片描述

应用场景

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

2.name属性

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

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

在这里插入图片描述

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

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

Spring5.x 与 日志框架 的整合

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

Spring5.x 整合 log4j:

  1. 引⼊ 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>

  1. ⼊ 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值