容器接口实现

 

BeanFactory的实现

 
public class TestBeanFactory { public static void main(String[] args) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // bean 的定义(class, scope, 初始化, 销毁) AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition(); beanFactory.registerBeanDefinition("config", beanDefinition); for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); } } @Configuration static class Config { @Bean public Bean1 bean1() { return new Bean1(); } @Bean public Bean2 bean2() { return new Bean2(); } @Bean public Bean3 bean3() { return new Bean3(); } @Bean public Bean4 bean4() { return new Bean4(); } } interface Inter { } static class Bean3 implements Inter { } static class Bean4 implements Inter { } static class Bean1 { private static final Logger log = LoggerFactory.getLogger(Bean1.class); public Bean1() { log.debug("构造 Bean1()"); } @Autowired private Bean2 bean2; public Bean2 getBean2() { return bean2; } @Autowired @Resource(name = "bean4") private Inter bean3; public Inter getInter() { return bean3; } } static class Bean2 { private static final Logger log = LoggerFactory.getLogger(Bean2.class); public Bean2() { log.debug("构造 Bean2()"); } } }
notion image
发现spring容器最后只有Config 这个bean对象@Bean注解并没有生效

不会主动调用 BeanFactory 后处理器

// 给 BeanFactory 添加一些常用的后处理器 AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
public static void main(String[] args) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // bean 的定义(class, scope, 初始化, 销毁) AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition(); beanFactory.registerBeanDefinition("config", beanDefinition); // 给 BeanFactory 添加一些常用的后处理器 AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory); for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); } }
notion image
容器中多了一些处理器对象
 
执行BeanFactory后置处理器
// BeanFactory 后处理器主要功能,补充了一些 bean 定义 beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactoryPostProcessor -> { beanFactoryPostProcessor.postProcessBeanFactory(beanFactory); });
notion image
打印bean1对象的成员变量bean2

不会主动添加 Bean 后处理器

System.out.println(beanFactory.getBean(Bean1.class).getBean2());
notion image
添加bean后处理器
public static void main(String[] args) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // bean 的定义(class, scope, 初始化, 销毁) AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition(); beanFactory.registerBeanDefinition("config", beanDefinition); // // 给 BeanFactory 添加一些常用的后处理器 AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory); // BeanFactory 后处理器主要功能,补充了一些 bean 定义 beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactoryPostProcessor -> { beanFactoryPostProcessor.postProcessBeanFactory(beanFactory); }); // Bean 后处理器, 针对 bean 的生命周期的各个阶段提供扩展, 例如 @Autowired @Resource ... beanFactory.getBeansOfType(BeanPostProcessor.class).values().stream() .forEach(beanPostProcessor -> { beanFactory.addBeanPostProcessor(beanPostProcessor); }); for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); } System.out.println(beanFactory.getBean(Bean1.class).getBean2()); }
notion image

不会主动初始化单例

getBean2的时候bean2开始构造
beanFactory.preInstantiateSingletons(); // 准备好所有单例 取消延迟加载 在beanFactory.getBean(Bean1.class) 之前完成bean1和bean2的构造
notion image
 

ApplicationContext的实现

ClassPathXmlApplicationContext

 
private static void testClassPathXmlApplicationContext() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("b01.xml"); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } System.out.println(context.getBean(Bean2.class).getBean1()); }
resource目录下的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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bean1" class="com.onethink.a02.A02Application.Bean1"/> <bean id="bean2" class="com.onethink.a02.A02Application.Bean2"> <property name="bean1" ref="bean1"/> </bean> </beans>
notion image

FileSystemXmlApplicationContext

加载文件需要指定文件系统中的路径
private static void testFileSystemXmlApplicationContext() { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("E:\\code\\java\\spring_learn\\show\\src\\main\\resources\\b01.xml"); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } System.out.println(context.getBean(Bean2.class).getBean1()); }

AnnotationConfigApplicationContext

基于注解配置类创建容器
private static void testAnnotationConfigApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } System.out.println(context.getBean(Bean2.class).getBean1()); } @Configuration static class Config { @Bean public Bean1 bean1() { return new Bean1(); } @Bean public Bean2 bean2(Bean1 bean1) { Bean2 bean2 = new Bean2(); bean2.setBean1(bean1); return bean2; } }
notion image

AnnotationConfigServletWebServerApplicationContext

基于配置类创建,用于web环境
private static void testAnnotationConfigServletServerApplicationContext() { AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(WebConfig.class); }
@Configuration static class WebConfig { @Bean public ServletWebServerFactory servletWebServerFactory() { return new TomcatServletWebServerFactory(); } @Bean public DispatcherServlet DispatcherServlet() { return new DispatcherServlet(); } @Bean public DispatcherServletRegistrationBean registrationBean(DispatcherServlet disp return new DispatcherServletRegistrationBean(dispatcherServlet, "/"); } @Bean("/hello") public Controller controller1() { return (request, response) -> { response.getWriter().print("hello"); return null; }; } }
 
notion image