通過Supplier創建對象示例

通過Supplier接口創建對象,具體code 如下:

package com.gientech.supplier;

public class Car {

    private String name;

    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                '}';
    }
}

package com.gientech.supplier;

public class CreateSupplier {
    public static Car createCar(){
        return new Car("AITO");
    }
}
package com.gientech.supplier;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class SupplierBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition car = beanFactory.getBeanDefinition("car");
        GenericBeanDefinition genericBeanDefinition = (GenericBeanDefinition) car;
        genericBeanDefinition.setInstanceSupplier(CreateSupplier::createCar);
        genericBeanDefinition.setBeanClass(Car.class);
    }
}

創建配置文件

<?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">

    <bean id="car" class="com.gientech.supplier.Car"></bean>
    <bean id="supplierBeanFactoryPostProcessor" class="com.gientech.supplier.SupplierBeanFactoryPostProcessor"></bean>

</beans>
<?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">

    <bean id="car" class="com.gientech.supplier.Car"></bean>
    <bean id="supplierBeanFactoryPostProcessor" class="com.gientech.supplier.SupplierBeanFactoryPostProcessor"></bean>

</beans>

創建啓動類

package com.gientech.supplier;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SupplierTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("supplier.xml");
        Car bean = ac.getBean(Car.class);
        System.out.println(bean.getName());
    }
}

運行結果如圖:
supplier接口

supplier 接口是函數接口,
執行流程如下圖所示:
supplier執行流程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章