beanfactory

1.applicationcontext的父接口

2.是Spring的核心容器

功能

表面只有getBean,但实现类默默发挥了巨大作用

1.管理所有bean

2.控制反转

3.基本的依赖注入

实现(后处理器)

package org.example;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import static java.rmi.server.LogStream.log;

public class TestBeanFactory {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //bean的定义(描述bean的特征)
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition();
        beanFactory.registerBeanDefinition("config", beanDefinition);
        System.out.println("原始已有的bean");
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        //添加一些常用的后处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
        System.out.println("添加一些常用的后处理器");
        for (String name : beanFactory.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        //调用后处理器
        System.out.println("调用后处理器");
        beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().forEach(beanFactoryPostProcessor -> {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        });
        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();
        }
    }

    @Component
    static class Bean1 {


        public Bean1() {
            log("构造Bean1()");
        }

        @Autowired
        private Bean2 bean2;

        public Bean2 getBean2() {
            return bean2;
        }
    }

    @Component
    static class Bean2 {

        public Bean2() {
            log("构造Bean2()");
        }
    }

}

applicationcontext

功能

1.继承了MessageSource,有了处理国际化资源的能力

context.getMessage("hi",null,locale.CHINA)//参数:翻译内容,配置,语言类型

2.继承了ResourcePatternResolver,有了根据通配符匹配多个资源的能力

context.getResource(classpath:application.properties)//参数:资源

3.继承了ApplicationEventPublisher,有了发布事件对象的能力

context.getEnvironment().getProperty("java_home")//环境变量名称

4.继承了EnvironmentCapable,有了读取、处理环境信息的能力,用于解耦

context.publishEvent(new UserRegisteredEvent(context));//参数:事件源

监听器(增加方法) 

//无返回值,名字随意,参数固定
@EventListener
public void aaa(UserRegisteredEvent event){
log.debug("{}",event)
}

实现

beanfactory和applicationcontext的关系

applicationcontext间接继承了beanfactory,applicationcontext把beanfactory作为成员变量

applicationcontext的功能更多

区别

相同:

Spring提供了两种不同的IOC容器,一个是BeanFactory,另外一个是ApplicationContext,它们都是Java interface, ApplicationContext继承于BeanFactory(ApplicationContext继承ListableBeanFactory。

它们都可以用来配置XML属性,也支持属性的自动注入。

而ListableBeanFactory继承BeanFactory),BeanFactory 和 ApplicationContext 都提供了一种方式,使用getBean("bean name")获取bean。

不同:

当你调用getBean()方法时,BeanFactory仅实例化bean,而ApplicationContext在启动容器的时候实例化单例bean,不会等待调用getBean()方法时再实例化。

BeanFactory不支持国际化,即i18n,但ApplicationContext提供了对它的支持。

BeanFactory与ApplicationContext之间的另一个区别是能够将事件发布到注册为监听器的bean。

BeanFactory 的一个核心实现是XMLBeanFactory 而ApplicationContext 的一个核心实现是
ClassPathXmlApplicationContext,Web容器的环境我们使用WebApplicationContext并且增加了getServletContext 方法。

如果使用自动注入并使用BeanFactory,则需要使用API注册AutoWiredBeanPostProcessor,如果使用ApplicationContext,则可以使用XML进行配置。

简而言之,BeanFactory提供基本的IOC和DI功能,而ApplicationContext提供高级功能,BeanFactory可用于测试和非生产使用,但ApplicationContext是功能更丰富的容器实现,应该优于BeanFactory

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐