关于spring:Spring延迟依赖注入ObjectFactoryObjectProvider

ObjectProvider 继承自 ObjectFactory

  • ObjectFactory提早注入 (繁多类型注入/汇合类型注入)
  • ObjectProvider提早注入 (繁多类型注入/汇合类型注入) 举荐

实体类Rumenz/SuperRumenz

package com.rumenz;

public class Rumenz{

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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


package com.rumenz;

public class SuperRumenz extends Rumenz {
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "SuperRumenz{" +
                "type='" + type + '\'' +
                "} " + super.toString();
    }
}

配置文件Beans.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
    <bean id="superRumenz" class="com.rumenz.SuperRumenz" parent="rumenz" primary="true">
        <property name="id" value="456"/>
        <property name="name" value="入门小站-子类"/>
        <property name="type" value="1"/>
    </bean>
</beans>

调用

package com.rumenz;


import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;
import java.util.Set;


public class DemoApplication  {
    //繁多类型注入,同一种类型存在多个,会抉择primary="true"注入
    @Autowired
    private ObjectFactory<Rumenz> rumenz;

    //繁多类型输出,应用Qualifier抉择特定的Bean
    @Autowired
    @Qualifier("rumenz")
    private ObjectProvider<Rumenz> rumenz1;

    //汇合类型注入
    @Autowired
    private ObjectProvider<Set<Rumenz>> set1;
    //汇合类型注入
    @Autowired
    private ObjectFactory<Set<Rumenz>> set2;

   
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac);
        xr.loadBeanDefinitions("Beans.xml");
        
        ac.register(DemoApplication.class);
        ac.refresh();
        DemoApplication demoApplication = ac.getBean(DemoApplication.class);

        System.out.println(demoApplication.rumenz.getObject().getName());
        System.out.println(demoApplication.rumenz1.getObject().getName());


        Set<Rumenz> lists1 = demoApplication.set1.getIfAvailable();

        System.out.println(Arrays.toString(lists1.toArray()));


        Set<Rumenz> lists2 = demoApplication.set2.getObject();

        System.out.println(Arrays.toString(lists2.toArray()));


        ac.close();
    }
}

输入

入门小站-子类
入门小站
[Rumenz{id=123, name='入门小站'}, SuperRumenz{type='1'} Rumenz{id=456, name='入门小站-子类'}]
[Rumenz{id=123, name='入门小站'}, SuperRumenz{type='1'} Rumenz{id=456, name='入门小站-子类'}]

源码:https://github.com/mifunc/Spr…

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理