spring的创建和使用
spring创建和使用的目标:
- spring 项目创建
- 将bean存储到spring(容器)
- 从spring中获取bean【DI】
1. 创建Spring项目(core)
1. 创建一个普通的maven项目
出现这两部分,说明maven项目初始化好了。
2. 添加spring框架支持
(spring-context、spring-beans)。
首先配置maven国内源
在maven仓库中查找spring
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.26</version>
</dependency>
</dependencies>
3. 添加启动类
为后面从spring中获取bean对象做准备
public class App {
public static void main(String[] args) {
}
}
2. 将bean对象存储到spring容器中
这里的存储是声明式的,真正的存储在第三大步的第1或第2步
- 创建一个bean。
什么是bean?
在Java中,一个对象如果被使用多次,就可以称为bean
public class Student {
public Student() {
System.out.println("init student");
}
public void sayHi(){
System.out.println("hi student");
}
}
- 将创建的bean存储到spring容器中
在resources下创建spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:content="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- <content:component-scan base-package="com.bit.service"></content:component-scan>-->
</beans>
<!-- 将 bean(com.spring.demo.Student)存储到spring容器,它的名称叫做student-->
<bean id="student" class="com.spring.demo.Student"></bean>
这里的id是自定义的
3. 从spring容器中读取到Bean (DI)
整体逻辑:
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 1.得到spring
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
// 2.从spring容器中获取bean对象
Student student = (Student) context.getBean("student"); // student 是 xml 中的 id
// 3.使用bean
student.sayHi();
}
}
1. 创建spring上下文(得到spring对象)
- 得到spring(上下文)对象 (在启动类上)
Spring 上下文对象可使用ApplicationContext
:
这里的名称必须和resources中的文件名相同
// 1.得到 Spring 的上下⽂对象,创建的时候需要配置 Spring 配置信息
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
ApplicationContext
:将xml中所有的对象存储到spring容器中,即一次性加载并初始化所有的bean对象。
在newApplicationContext
上面这一步时就已经初始化了
特征:比较费内存,但是一次性加载后,之后的读取会非常的快BeanFactory
:除了ApplicationContext
之外,我们还可以使用BeanFactory
来作为 Spring 的上下文。
BeanFactory
:lazy load,需要哪个才去加载。【不建议使用了】
特征:节省内存,但调用时才加载初始化 bean 到 spring 中,效率不高。
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
ApplicationContext 和 BeanFactory的区别(常见面试题)
- 继承关系和功能方面来说:Spring 容器有两个顶级的接口:
BeanFactory
和ApplicationContext
。BeanFactory
提供了基础的访问容器的能力。【功能少】ApplicationContext
属于BeanFactory
的子类,它除了继承了BeanFactory
的所有功能之外,它还拥有独特的特性,还添加了对国际化支持、资源访问支持、以及事件传播等方面的支持。【功能多】
- 从性能方面来说:
ApplicationContext
是一次性加载并初始化所有的 Bean 对象。BeanFactory
是需要那个才去加载那个,因此更加轻量。
2. getBean方法获取bean对象
- 从spring容器中获取到bean对象
这里的名称必须和spring-config.xml的id名称一致
Student student1 = (Student) context.getBean("student");
获取bean的3种方式
- 通过名称获取(bean的id)
Student student1 = (Student) context.getBean("student");
- 通过类型方式获取
Student student1 = context.getBean(Student.class);
虽然简洁,但如果spring中一个类型存储了多个实例,那么使用类型获取bean就会报错:
Exception in thread “main” org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘org.example.Student’ available: expected single matching bean but found 2: student,stu
- 根据类型和名称一起获取【推荐】
Student student1 = context.getBean("student", Student.class); //根据名称+类型获取对象
spring中一个类型存储的多个实例是不相同的,不是一个对象,是两个实例对象
<bean id="student" class="com.spring.demo.Student"></bean>
<bean id="stu" class="com.spring.demo.Student"></bean>
Student stu = context.getBean("stu", Student.class); //根据名称+类型获取对象
Student student1 = context.getBean("student", Student.class); //根据名称+类型获取对象
System.out.println("stu == student1 : " + (stu == student1)); //false
3. 使用bean对象(非必须)
student1.sayHi();