Skip to content

一、FactoryBean 实例

1. 创建一个 Deep 对象类

java
package org.example.spring5.domain;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 17:26 <br>
 * @since: 1.0 <br>
 * @description: Deep <br>
 */
public class Deep {

    public Deep() {
        System.out.println("【Deep系统】将被注入到Spring IoC容器中...");
    }
}

2. 创建一个 DeepFactoryBean 类实现 FactoryBean 接口 来实现 Deep 对象的注入

java
package org.example.spring5.config.register;

import org.example.spring5.domain.Deep;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author: Calvin <br>
 * @date: 2020/11/26 16:04 <br>
 * @since: 1.0 <br>
 * @description: DeepFactoryBean 通过 FactoryBean 接口,实现对象的注入<br>
 */
public class DeepFactoryBean implements FactoryBean<Deep> {

    /**
     * 获取注入的对象
     *
     * @return Deep 深度操作系统
     * @throws Exception
     */
    @Override
    public Deep getObject() throws Exception {
        // 创建 Deep 对象
        return new Deep();
    }

    /**
     * 获取对象的类型
     * @return Deep 编译后的 Class 文件
     */
    @Override
    public Class<?> getObjectType() {
        return Deep.class;
    }

}

3. 创建一个 DeepFactoryBean 类实现 FactoryBean 接口 来实现 Deep 对象的注入

java
package org.example.spring5.config.register;

import org.example.spring5.domain.Deep;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author: Calvin <br>
 * @date: 2020/11/26 16:04 <br>
 * @since: 1.0 <br>
 * @description: DeepFactoryBean 通过 FactoryBean 接口,实现对象的注入<br>
 */
public class DeepFactoryBean extends Deep implements FactoryBean<Deep> {

    /**
     * 获取注入的对象
     *
     * @return Deep 深度操作系统
     * @throws Exception
     */
    @Override
    public Deep getObject() throws Exception {
        // 创建 Deep 对象
        return new Deep();
    }

    /**
     * 获取对象的类型
     * @return Deep 编译后的 Class 文件
     */
    @Override
    public Class<?> getObjectType() {
        return Deep.class;
    }
}

4. 在 SpringConfiguration 配置类中,通过 @Bean 注入 DeepFactoryBean 类对象。

java
package org.example.spring5.config;

import org.example.spring5.config.annotations.EnableOS;
import org.example.spring5.config.register.DeepFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:18 <br>
 * @since: 1.0 <br>
 * @description: SpringConfig <br>
 */
@Configuration 
@EnableOS
@ComponentScan(value = "org.example.spring5") 
public class SpringConfiguration {

    @Bean
    public DeepFactoryBean deepFactoryBean() {
        return new DeepFactoryBean();
    }

}

5. 启动程序

java
package org.example.spring5;

import org.example.spring5.config.SpringConfiguration;
import org.example.spring5.config.register.DeepFactoryBean;
import org.example.spring5.domain.Deep;
import org.example.spring5.domain.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * @author: Calvin <br>
 * @date: 2020/11/25 14:27 <br>
 * @since: 1.0 <br>
 * @description: SpringApplication <br>
 */
public class SpringApplication {

    private static AnnotationConfigApplicationContext annotationConfigApplicationContext;

    public static void main(String[] args) throws Exception {
        annotationConfigApplicationContext  = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        System.out.println("Bean 对象已装配完毕...");

        Deep DeepBean = (Deep) annotationConfigApplicationContext.getBean(DeepFactoryBean.class);
        System.out.println(DeepBean);
    }
}

6. 输出结果

log
【Win7操作系统】将被注入到Spring IoC容器中...
【Win10操作系统】将被注入到Spring IoC容器中...
【Deep系统】将被注入到Spring IoC容器中...
【Mac系统】将被注入到Spring IoC容器中...
Bean 对象已装配完毕...
org.example.spring5.config.register.DeepFactoryBean@f736069

二、 扩展:FactoryBean 和 BeanFactory 区别

  • FactoryBean 作用 注册 Bean 对象
  • BeanFactory 作用 获取 Bean 对象