Loading

通过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执行流程

posted @ 2024-04-14 14:41  zgcy123456  阅读(56)  评论(0)    收藏  举报