Spring的工厂类

image-20210402112816379

实例

FileSystemXmlApplicationContext

读取磁盘系统中的配置文件

  • 目录结构

    image-20210402113004998

  • 任意路径下存放配置xml文件:applicationContext.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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">

    <!--UserService的创建权交给了Spring-->
    <bean id="userService" class="com.example.factory.demo.service.UserServiceImpl">
    <!--设置属性-->
    <property name="name" value="任意路径"/>
    </bean>
    </beans>
  • 编写代码,读取磁盘系统中的配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.example.factory.demo;

    import com.example.factory.demo.service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    /**
    * 读取磁盘系统中的配置文件
    *
    * @author jinglv
    * @date 2020/12/27
    */
    public class FactoryFileTest {
    public static void main(String[] args) {
    // 创建Spring的工厂类
    ApplicationContext context = new FileSystemXmlApplicationContext("spring-bean-factory/applicationContext.xml");
    // 通过工厂类获得类
    UserService userService = (UserService) context.getBean("userService");
    userService.sayHello();
    }
    }
  • 执行结果

    image-20210402113052813

BeanFactory

传统方式工厂类(已过时)

  • 读取配置文件

    • ClassPathResource:工程目录(resources目录下)
    • FileSystemResource:本地磁盘目录
  • resources目录下存放配置xml文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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">

    <!--UserService的创建权交给了Spring-->
    <bean id="userService" class="com.example.factory.demo.service.UserServiceImpl">
    <!--设置属性-->
    <property name="name" value="factory"/>
    </bean>
    </beans>
  • 编写代码,读取磁盘系统中的配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.example.factory.demo.service;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;

    /**
    * 传统方式工厂类:BeanFactory
    *
    * @author jinglv
    * @date 2020/12/27
    */
    public class FactoryBeanTest {

    public static void main(String[] args) {
    // 创建工厂类
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    // 通过工厂类获得类
    UserService userService = (UserService) beanFactory.getBean("userService");
    userService.sayHello();
    }
    }

    注意:BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(“spring-bean-factory/applicationContext.xml”)); 则是本地磁盘路径

  • 执行结果

    image-20210402113246519

总结

1. 工厂类 BeanFactory 和 ApplicationContext 的区别

  • ApplicationContext 是 BeanFactory 的子接口,提供了比父接口更多的功能。
  • 在生成 bean 实例的时候,生成的时机是不一样的。
  • BeanFactory 在工厂实例化后,在调用 getBean 时创建实例。
  • ApplicationContext 在一加载配置文件的时候,将配置文件中所有单例模式生成的类全部实例化。

注意:现在一般使用 ApplicationContext,不建议使用 BeanFactory。

2. 加载配置文件的两种方式

  • 工作目录下加载:ClassPathXmlApplicationContext
  • 某个目录下加载:FileSystemXmlApplicationContext