前言
Spring Bean 可以通过两种主要方式定义:基于 XML 配置文件和基于注解。今天我们讲解基于 XML 配置文件来定义 Bean ,在 XML 配置文件中,使用 <bean> 元素定义 Bean,描述 Bean 的创建、配置和依赖关系,并存储与使用。
目录
1.2 添加Spring框架的支持(添加pom.xml文件)
2.4 ApplicationContext与BeanFactory的区别
1. Spring项目的创建
1.1 创建一个Maven项目
第一步
第二步
第三步
1.2 添加Spring框架的支持(添加pom.xml文件)
创建好 Spring 项目后,项目里面会默认出现一个 pom.xml 文件,我们需要把 Spring 的框架支持添加进去,也就是将以下代码复制到 pom.xml。
注意,在 pom.xml 添加框架支持是为了简化项目依赖管理和提高开发效率。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
1.3 添加Sping的配置文件
添加 Spring 的配置文件,我们通常把这个配置文件命名为 spring-config.xml 并且把该文件创建到 resources 文件夹中。后面,我们将也是 Bean 对象注册到该配置文件里面。
<?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">
</beans>
2. Spring的使用
2.1 存储Bean对象
- 为了存储 Bean 对象首先我们得要有一个 Bean 对象
- 将创建好的 Bean 对象存储到 Spring 容器当中
首先,我们在 java 底下创建一个包,并在包里面创建一个 Student 类,此时的 Student 类就是一个我们创建好的 Bean 对象。:
package com.study.spring;
public class Student {
public void hello() {
System.out.println("welcome use Student!");
}
}
其次,在 java 底下创建一个启动类 App,如下如所示:
public class App {
public static void main(String[] args) {
}
}
最后,我们将上面创建的 Bean 对象存储到 Spring 容器当中,也就是我们创建的 spring-config.xml(spring配置文件) 之中。
我们打开 spring-config.xml 并通过 <bean></bean> 标签来存储。
<?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="student" class="com.study.spring.Student"></bean>
</beans>
其中 id 为我们后面获取 Bean 对象所设置的一个名称(任意起名),class 为这个 Bean 对象所在的位置。
2.2 获取Bean对象
- 为了获取到 Bean 对象,我们就得获取到 Spring 的上下文,因为我们将 Bean 对象交给 Spring 管理了,可通过 ApplicationContext 和 BeanFactory 来获取。
- 可用过 <bean></bean> 标签中的 id 来获取到一个指定的 Bean 对象。
2.3 使用Bean对象
在启动类中,通过 ApplicationContext 来获取 Spring 上下文并使用。
import com.study.spring.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
// 1.获取 Spring 的上下文对象
ApplicationContext context = new
ClassPathXmlApplicationContext("spring-config.xml");
// 2.加载到 id 为 student 的 Bean 中
Student student = (Student) context.getBean("student");
// 3.运行 Student 类中 hello 方法
student.hello();
}
}
结果如下:
使用 ApplicationContext 为获取 Spring 上下文对象的方法。ClassPathXmlApplicationContext("spring-config.xml")为加载一个名为 spring-config.xml 的 Spring 配置文件。
通过 BeanFactory 来获取 Spring 上下文
public class App {
public static void main(String[] args) {
// 1.获取 Spring 的上下文对象
BeanFactory beanFactory =
new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
// 2.加载到 id 为 student 的 Bean 中
Student student = (Student) beanFactory.getBean("student");
// 3.运行 Student 类中 hello 方法
student.hello();
}
}
结果如下:
2.4 ApplicationContext与BeanFactory的区别
- 从继承关系和功能方面:Spring 容器有两个接口即 BeanFactory 和 ApplicationContext。其中 BeanFactory 提供了基础的访问容器的功能,而 ApplicationContext 为 BeanFactory 的子类,它继承了 BeanFactory 的所有功能之外,还添加了对国际化的支持、资源访问、以及事件传播等等方面的支持。
- 从性能来说:ApplicationContext 是一次性加载并初始化所有的 Bean 对象,而BeanFactory 是需要那个就加载那个,因此更轻量。
以上就是今天的博客内容,下期我们讲解更加简单的存储与使用 Bean 对象。
🥊作者:一只爱打拳的程序猿,Java领域新星创作者,优快云、阿里云社区优质创作者