4.Beanfactory 快速入门

Beanfactory和Application_spring

  1. 导入Spring的Jar包或者Maven坐标;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
  1. 定义UserService接口以及UserServiceImpl实现类;
  2. 创建beans.xml配置文件,将UserServiceImpl的信息配置到xml中;
<?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">

<!--配置UserServiceImpl-->
<bean id="userService" class="com.mink.service.Impl.UserServiceImpl"></bean>

</beans>
  1. 编写测试程序,创建BeanFactory,加载配置文件,获取User Service实例对象。
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions("beans.xml");

UserService userService = (UserService) beanFactory.getBean("userService");
System.out.println(userService);
}
<bean id="userService" class="com.mink.service.Impl.UserServiceImpl">
<property name ="userDao" ref="userDao"></property>
</bean>

<bean id = "userDao" class="com.mink.dao.Impl.UserDaoImpl"></bean>

上面使用BeanFactory完成了IoC思想的实现,下面实现DI依赖注入:

  1. 定义UserDao接口机器UserDaoImpl实现类;
  2. 修改UserServiceImpl代码,添加一个setUserDao(UserDao userDao)用于接收注入的对象;
  3. 修改beans.xml配置文件,再UserDaoImpl的<bean>中嵌入<property>配置注入;
  4. 修改测试代码,获得执行UserService时,setUserService发给发执行流注入操作。

5.ApplicationContext快速入门

public class ApplicationContextTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
System.out.println(userService);

}
}

6.BeanFactory与ApplicationContext的关系

  1. BeanFactory是spring的早期接口,称为Spring的Bean工厂,ApplicationContext是后期更高级接口,称之为spring容器;
  2. ApplicationContext在BeanFactory基础上对功能进行了扩展,例如:监听功能、国际化功能等。BeanFactory的API更偏向底层,ApplicationContext的API大多数是对这些底层API的封装;
  3. Bean创建的主要逻辑和功能都被封装在BeanFactory中,ApplicationContext与BeanFactory既有继承关系,又有融合关系。
  4. Bean的初始化时机不同,原始BeanFactory实在首次调用getBean时进行Bean的创建,而ApplicationContext则是配置文件加载,容器一创建就将Bean都实例化并初始化好。

Java中接口多继承

ApplicationContext接口同时继承了好多个接口

Java 中不允许类的多继承是为了避免:A 同时继承了 B 和 C 两个类,并且这两个类中都有一个同名方法D,此时在 A 的实例中调用 D 方法就不知道该运行哪一个。而接口中就没有这种困扰,因为都是抽象方法,没有方法体,在实现类中都要实现方法,就不存在歧义。

7.BeanFactory的继承体系

BeanFactory是核心接口,项目运行过程中肯定有具体实现参与,这个具体实现就是DefaultListableBeanFactory,而ApplicationContext内部维护的BeanFactory的类也是它。

Beanfactory和Application_spring_02

8.ApplicationContext的继承体系

在spring基础环境下,即只导入spring时的继承体系

Beanfactory和Application_spring_03

只在Spring基础环境下,常用的三个ApplicationContext作用如下:

实现类

功能描述

ClassPathXmlApplicationContext

加载类路径下的xml配置的ApplicationContext

FileSystemXmlApplicationContext

加载磁盘路径下的xml配置的ApplicationContext(用在绝对路径)

AnnotationConfigApplicationContext

加载注解配置类的ApplicationContext