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
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">
调用
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;
//单一类型输入,使用Qualifier选择特定的Bean
@Autowired
@Qualifier("rumenz")
private ObjectProvider rumenz1;
//集合类型注入
@Autowired
private ObjectProvider> set1;
//集合类型注入
@Autowired
private ObjectFactory> 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 lists1 = demoApplication.set1.getIfAvailable();
System.out.println(Arrays.toString(lists1.toArray()));
Set 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='入门小站-子类'}]