目录
ApplicationContext和BeanFactory的区别
Spring作为一个包含众多工具并且可以对这些工具进行管理的IoC容器,它有两个重要的作用,那就是存储对象和使用对象。要使用它的功能则必须将Spring创建出来,声明:本篇博客所讨论的Spring的创建和使用,是基于Maven来创建的,现在基本上没怎么用了,现在都是用SpringBoot直接创建Spring项目。虽说现在用的不多,但是对于帮助理解Spring还是很有帮助的,后面的博客还会讨论Spring的创建和使用2.0(也是基于Maven来创建的)。
PS:Spring框架的发展历程
Spring框架->Maven XML->Spring Boot->Spring Cloud
1.创建Spring项目
2.将对象存储到Spring框架
3.将对象从Spring框架中读出来
创建Spring项目的过程和Servlet大致类似,主要包含以下三个步骤:
1.创建一个Maven项目;
2.添加Spring的依赖到pom.xml文件中;
3.添加启动类;
这里的过程很简单,就不演示了,但是创建好有一点一定要注意:
检查setting里面的Maven有没有勾选User setting file(用户配置文件)、Local repository(本地存储库),像下面这样:

勾选用户配置文件的目的是为了:让Maven在下载依赖的时候,使用本地的配置文件,而且本地配置文件里面一定要配置国内源,也就是让Maven下载依赖从国内下载。默认配置文件里面的源都是国外的,下载会很慢而且很有可能会失败。
勾选本地存储库就不用说了吧,Local repository是用来设置保存依赖(jar包)路径的,不勾选怎么保存啊。
还有一点,可能有的人他没有settings.xml文件,那就的去网上下载了,下载之后记得将镜像改为国内的。
将下面的依赖拷贝到pom.xml文件中,然后不要忘了刷新Maven
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.3.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-beansartifactId>
- <version>5.2.3.RELEASEversion>
- dependency>
- dependencies>
创建启动类是为了测试Spring的功能。这个就随便在java目录下面创建一个类就行了:

因为当前这个类是放在框架中的,而现在的框架就是Spring,因此当前这个类启动的时候,Spring框架也就启动了 。
首先Bean对象就是java中的普通对象,这里因为是将对象存储在Spring中,所以将这些对象比喻成豆子,所以就称为了Bean对象。存储Bean对象,主要分为两步:创建Bean、将Bean注册到Spring容器中。

在创建好的项目中添加Spring配置文件到resources目录下面,并且在配置文件中加入以下代码:
- "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>

然后再将User对象注册到Spring中就可以了,也就是在配置文件中加入一行代码:
- <beans>
- <bean id="user" class="model.UserBean">bean>
- beans>
上面这段代码的作用就是将对象存储到Spring框架中。而其中的id是对象的标识符,用于读取对象;class用来表示那个对象要保存在Spring中。

获取使用Bean对象分为三步:创建Spring的上下文、获取指定的Bean、使用Bean
因为对象都交给Spring管理了,所以要想获取到对象,就必须先得到Spring的上下文。
在App文件中通过ApplicationContext来获取:
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class App {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
- }
- }
上面 ClassPathXmlApplicationContext属于ApplicationContext的子类,拥有ApplicationContext的所有功能。
获取Spring的上下文还有另一种方式,通过BeanFactory来获取:
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
-
- public class App {
- public static void main(String[] args) {
- BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
- }
- }
ApplicationContext和BeanFactory效果是一样的。而BeanFactory是一个接口,ApplicationContext是BeanFactory的子类 。
方式一,通过bean的id来获取:
- public class App {
- public static void main(String[] args) {
- //1.得到Spring上下文对象
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
- //2.加载某个Bean
- UserBean user = (UserBean) context.getBean("user");
- }
- }
这里的"user"就对应着注册对象中的id,因为context.getBean返回的类型是Object,所以要强转为UserBean类型。
该方式的缺点就是需要类型转换。
方式二,通过类型获取:
- public class App {
- public static void main(String[] args) {
- //1.得到Spring上下文对象
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
- //2.加载某个Bean
- UserBean user = context.getBean(UserBean.class);
- }
- }
这种方式的优点,无需类型转换;
缺点:对于多个对象的同一种类型的Bean获取会报错;


方式三,通过id+类型的方式来获取bean:
- public class App {
- public static void main(String[] args) {
- //1.得到Spring上下文对象
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
- //2.加载某个Bean
- UserBean user = context.getBean("user",UserBean.class);
- }
- }
优点:无需类型转换,对于多个对象指向一个类的情况不会报错。
- import model.UserBean;
- 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.加载某个Bean
- UserBean user = context.getBean("user",UserBean.class);
- //3.使用Bean
- System.out.println(user.hello("李华"));
- }
- }

总结Spring的创建和使用:
