单一类型依赖查找接口 - BeanFactory
BeanFacotry是spring中比较原始的Factory,BeanFactory是个Factory,也就是IOC容器或对象工厂
根据Bean名称查找
getBean(String)
覆盖默认参数getBean(String,Object)
根据Bean类型查找
Bean实时查找
getBean(Class)
覆盖默认参数 getBean(Class,Object)
Bean延时查找
getBeanProvider(Class)
getBeanProvider(ResolvableType)
根据Bean名称和Bean类型查找
getBean(String,Class)
实体类Rumenz.java
package com.rumenz;
public class Rumenz {
private Integer id;
private String name;
@Override
public String toString() {
return "Rumenz{" +
"id=" + id +
", name='" + 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;
}
}
根据Bean名称查找
package com.rumenz;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
ac.register(DemoApplication.class);
ac.refresh();
//根据名称查找
Rumenz rumenz= (Rumenz) ac.getBean("rumenz");
System.out.println(rumenz);
ac.close();
}
@Bean
public Rumenz rumenz(){
Rumenz rumenz = new Rumenz();
rumenz.setId(123);
rumenz.setName("入门小站");
return rumenz;
}
}
根据类型查找
package com.rumenz;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
ac.register(DemoApplication.class);
ac.refresh();
//根据类型查找
Rumenz rumenz = ac.getBean(Rumenz.class);
System.out.println(rumenz);
ac.close();
}
@Bean
public Rumenz rumenz(){
Rumenz rumenz = new Rumenz();
rumenz.setId(123);
rumenz.setName("入门小站");
return rumenz;
}
}
Bean延时查找 getBeanProvider
package com.rumenz;
import java.util.Iterator;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
ac.register(DemoApplication.class);
ConfigurableListableBeanFactory beanFactory = ac.getBeanFactory();
ac.refresh();
ObjectProvider obj = beanFactory.getBeanProvider(Rumenz.class);
Iterator iterator = obj.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
ac.close();
}
@Bean
public Rumenz rumenz(){
Rumenz rumenz = new Rumenz();
rumenz.setId(123);
rumenz.setName("入门小站");
return rumenz;
}
}
根据Bean名称和Bean类型查找
package com.rumenz;
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
ac.register(DemoApplication.class);
ac.refresh();
//根据Bean名称和Bean类型查找
Rumenz rumenz = ac.getBean("rumenz",Rumenz.class);
System.out.println(rumenz);
ac.close();
}
@Bean
public Rumenz rumenz(){
Rumenz rumenz = new Rumenz();
rumenz.setId(123);
rumenz.setName("入门小站");
return rumenz;
}
}