Spring源码学习(十)---FactoryBean的解析

这里主要是使用org.springframework:spring-beans:5.2.0.RELEASE进行分析


1. 查看源码相关的快捷键

快捷键作用
Ctrl + Shift+i出现类似于预览的小窗口
Ctrl + Enter(接上步)完全打开源码
Ctrl + 鼠标左键一步到位打开源码 = Ctrl + Shift+i –>Ctrl + Enter
Alt+7查看类的有什么方法
ctrl+f12查看继承方法
ctrl+h查看接口的实现类
alt+7查看类的有什么方法
2下shift全局搜索整个项目

一.FactoryBean接口

  1. Spring 为此提供了一个org.springframework.beans.factory.FactoryBean的工厂类接口,用户可以通过实现该接口定制实例化bean的逻辑。
  2. 可以返回bean的实例的工厂bean,通过实现该接口可以对bean进行一些额外的操作,例如根据不同的配置类型返回不同类型的bean,简化xml配置等。

1. FactoryBean接口 源码

public interface FactoryBean<T> {
	String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
 	//返回由 FactoryBean 创建的 bean 实例,如果 isSingleton()返回 true,则该实例会放到Spring容器中单实例缓存池中
	@Nullable
	T getObject() throws Exception;


//返回 FactoryBean 创建的 bean 类型
	Class<?> getObjectType();

//返回由FactoryBean创建的bean实例的作用域是singleton还是 prototype。
	default boolean isSingleton() {
		return true;
	}
}

  1. 展示部分实现类

在这里插入图片描述

1. FactoryBean的使用方式

package com.xizi.pojo;


public class Car {
    private int maxSpeed;
    private String brand;
    private double price;
    // get/set 方法


    public Car() {
    }

    public Car(int maxSpeed, String brand, double price) {
        this.maxSpeed = maxSpeed;
        this.brand = brand;
        this.price = price;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "maxSpeed=" + maxSpeed +
                ", brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

CarFactoryBean 实现了FactoryBean接口,是一个FactoryBean

package com.xizi;

import com.xizi.pojo.Car;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

@Component
public class CarFactoryBean implements FactoryBean<Car> {

    private String carInfo;

    @Nullable
    public Car getObject() throws Exception {
        Car car = new Car();
        //按逗号分割字符串
        String[] infos = carInfo.split(",");
        car.setBrand(infos[0]);
        car.setMaxSpeed(Integer.valueOf(infos[1]));
        car.setPrice(Double.valueOf(infos[2]));
        return car;
    }

    @Nullable
    public Class<?> getObjectType() {
        return Car.class;
    }


    public boolean isSingleton() {
        return false;
    }

    // 接受逗号分隔符设置属性信息
    public void setCarInfo(String carInfo) {
        this.carInfo = carInfo;
    }
}

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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="beans.xml"/>
    <import resource="beans2.xml"/>


    <bean id="car" class="com.xizi.CarFactoryBean"  p:carInfo="劳斯莱斯,4000,500000" />

</beans>

测试

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("aplicationContext.xml");

        Car car = (Car) context.getBean("car");
        System.out.println(car);
     	System.out.println(context.getBean("&car"));

在这里插入图片描述

分析

context.getBean(“car”);获取到的是CarFactoryBean产生的实例也就是Car类的实例;而context.getBean("&car")获取到的是CarFactoryBean自己的实例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值