1、BeanFactory是Spring 底层IoC容器
(1)创建DefaultListableBeanFactory容器,通过源码可以看出其继承了BeanDefinitionRegistry
(2)读取配置文件
通过XmlBeanDefinitionReader加载配置,通过源码得知,XmlBeanDefinitionReader的构造函数的参数为BeanDefinitionRegistry:
引起可以直接把DefaultListableBeanFactory直接装配进去
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
package org.binsoft.thinking.in.spring.ioc.overview.container;
import org.binsoft.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import java.util.Map;
/**
* {@link BeanFactory} 作为IoC容器示例
*
* @author Administrator
* @version 1.0
* @date 2021/1/2 20:47
*/
public class BeanFactoryAsIoCContainerDemo {
public static void main(String[] args) {
//创建BeanFactory容器
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
//XML 配置文件 ClassPath 路径
String location = "classpath:/META-INF/dependency-lookup-context.xml";
//加载配置
int beanDefinitionsCount = reader.loadBeanDefinitions(location);
System.out.println("Bean 定义加载的数量: " + beanDefinitionsCount);
//不利用ApplicationContext,也可以加载文件
lookupCollectionByType(beanFactory);
}
private static void lookupCollectionByType(BeanFactory beanFactory) {
if (beanFactory instanceof ListableBeanFactory) {
ListableBeanFactory listableBeanfactory = (ListableBeanFactory) beanFactory;
Map<String, User> users = listableBeanfactory.getBeansOfType(User.class);
System.out.println("查找到的所有的 User 集合对象:" + users);
}
}
}
2、ApplicationContext 是具备应用特性的BeanFactory超集
package org.binsoft.thinking.in.spring.ioc.overview.container;
import org.binsoft.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
/**
* 注解能力 {@link ApplicationContext} 作为IoC容器示例
*
* @author Administrator
* @version 1.0
* @date 2021/1/2 20:47
*/
//标注Configuration注解 被CGLib 提升
@Configuration
public class AnnotationApplicationContextAsIoCContainerDemo {
public static void main(String[] args) {
//创建BeanFactory容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//将当前类作为配置类
applicationContext.register(AnnotationApplicationContextAsIoCContainerDemo.class);
//启动应用上下文
applicationContext.refresh();
//不利用ApplicationContext,也可以加载文件
lookupCollectionByType(applicationContext);
}
/**
* 通过Java注解定义一个Bean
* @return
*/
@Bean
public User user(){
User user = new User();
user.setId(8l);
user.setName("李淳罡");
return user;
}
private static void lookupCollectionByType(BeanFactory beanFactory) {
if (beanFactory instanceof ListableBeanFactory) {
ListableBeanFactory listableBeanfactory = (ListableBeanFactory) beanFactory;
Map<String, User> users = listableBeanfactory.getBeansOfType(User.class);
System.out.println("查找到的所有的 User 集合对象:" + users);
}
}
}